Top 60 JavaScript Interview Questions & Answers


1.What is JavaScript?
JavaScript is a client-side as well as server side scripting language that can be inserted into HTML pages
and is understood by web browsers. JavaScript is also an Object based Programming language

2.Enumerate the differences between Java and JavaScript?
Java is a complete programming language. In contrast, JavaScript is a coded program that can be
introduced to HTML pages. These two languages are not at all inter-dependent and are designed for the
different intent. Java is an object – oriented programming (OOPS) or structured programming language
like C++ or C whereas JavaScript is a client-side scripting language.

3.What are JavaScript Data Types?
Following are the JavaScript Data types:
 Number
 String
 Boolean
 Null
 Object
 Undefined
 Array

4.What is the use of isNaN function?
isNaN function returns true if the argument is not a number otherwise it is false.

5.Between JavaScript and an ASP script, which is faster?
JavaScript is faster. JavaScript is a client-side language and thus it does not need the assistance of the
web server to execute. On the other hand, ASP is a server-side language and hence is always slower than
JavaScript. Javascript now is also a server side language (nodejs).

6.Which company developed JavaScript?
Netscape is the software company who developed JavaScript.

7.What are undeclared and undefined variables?
Undeclared variables are those that do not exist in a program and are not declared. If the program tries
to read the value of an undeclared variable, then a runtime error is encountered.
Undefined variables are those that are declared in the program but have not been given any value. If the
program tries to read the value of an undefined variable, an undefined value is returned.

8.What is callback?
A callback is a plain JavaScript function passed to some method as an argument or option. Some
callbacks are just events, called to give the user a chance to react when a certain state is triggered.

9.How to create an object?
An object in JavaScript can be created using two was:
New Key word:
To create a student object from the above student class we can call the Student function using new
var student1 = new Student(‘santosh’,2)
Anonymous Object:
Anonymous objects can be created using pair of curls’ braces containing property name and value pairs.
Var rose = {‘color’: ‘red’}

  1. What are global variables? How are these variable declared and what are the problems associated
    with using them?
    Global variables are those that are available throughout the length of the code, that is, these have no
    scope. The var keyword is used to declare a local variable or object. If the var keyword is omitted, a
    global variable is declared.
    // Declare a global globalVariable = “Test”;
    The problems that are faced by using global variables are the clash of variable names of local and global
    scope. Also, it is difficult to debug and test the code that relies on global variables.

  2. What is a prompt box?
    A prompt box is a box which allows the user to enter input by providing a text box. Label and box will be
    provided to enter the text or number.

  3. What is ‘this’ keyword in JavaScript?
    ‘This’ keyword refers to the object from where it was called.

  4. Explain the working of timers in JavaScript? Also elucidate the drawbacks of using the timer, if
    Timers are used to execute a piece of code at a set time or also to repeat the code in a given interval of
    time. This is done by using the functions setTimeout, setInterval and clearInterval.
    The setTimeout(function, delay) function is used to start a timer that calls a particular function after the
    mentioned delay. The setInterval(function, delay) function is used to repeatedly execute the given
    function in the mentioned delay and only halts when cancelled. The clearInterval(id) function instructs
    the timer to stop.
    Timers are operated within a single thread, and thus events might queue up, waiting to be executed.

  5. Which symbol is used for comments in Javascript?
    // for Single line comments and
    /* Multi

  6. What is === operator?
    === is called as strict equality operator which returns true when the two operands are having the same
    value without any type conversion.

  7. Explain how can you submit a form using JavaScript?
    To submit a form using JavaScript use document.form[0].submit();

  8. How can the style/class of an element be changed?
    It can be done in the following way:
    document.getElementById(“myText”).style.fontSize = “20″;
    document.getElementById(“myText”).className = “anyclass”;

  9. Explain the difference between “==” and “===”?
    “==” checks only for equality in value whereas “===” is a stricter equality test and returns false if either
    the value or the type of the two variables are different.

  10. What would be the result of 3+2+”7″?
    Since 3 and 2 are integers, they will be added numerically. And since 7 is a string, its concatenation will
    be done. So the result would be 57.

  11. Explain how to detect the operating system on the client machine?
    In order to detect the operating system on the client machine, the navigator.appVersion string
    (property) should be used.

  12. What do mean by NULL in Javascript?
    The NULL value is used to represent no value or no object. It implies no object or null string, no valid
    boolean value, no number and no array object.

  13. What is the function of delete operator?
    The functionality of delete operator is used to delete all variables and objects in a program but it cannot
    delete variables declared with VAR keyword.

  14. What is an undefined value in JavaScript?
    Undefined value means the
    Variable used in the code doesn’t exist
    Variable is not assigned to any value
    Property doesn’t exist

  15. Explain what is pop()method in JavaScript?
    The pop() method is similar as the shift() method but the difference is that the Shift method works at
    the start of the array. Also the pop() method take the last element off of the given array and returns it.
    The array on which is called is then altered.
    var cloths = [“Shirt”, “Pant”, “TShirt”];
    //Now cloth becomes Shirt,Pant

  16. What is break and continue statements?
    Break statement exits from the current loop.
    Continue statement continues with next statement of the loop.

  17. What is the use of type of operator?
    ‘Typeof’ is an operator which is used to return a string description of the type of a variable.

  18. What is the use of Push method in JavaScript?
    The push method is used to add or append one or more elements to the end of an Array. Using this
    method, we can append multiple elements by passing multiple arguments.

  19. What is unshift method in JavaScript?
    Unshift method is like push method which works at the beginning of the array. This method is used to
    prepend one or more elements to the beginning of the array.

  20. What is the ‘Strict’ mode in JavaScript and how can it be enabled?
    Strict Mode adds certain compulsions to JavaScript. Under the strict mode, JavaScript shows errors for a
    piece of codes, which did not show an error before, but might be problematic and potentially unsafe.
    Strict mode also solves some mistakes that hamper the JavaScript engines to work efficiently.
    Strict mode can be enabled by adding the string literal “use strict” above the file.

  21. What is the way to get the status of a CheckBox?
    The status can be acquired as follows –
    If the CheckBox will be checked, this alert will return TRUE.

  22. Describe the properties of an anonymous function in JavaScript?
    A function that is declared without any named identifier is known as an anonymous function. In general,
    an anonymous function is inaccessible after its declaration.

  23. Is JavaScript case sensitive? Give an example?
    Yes, JavaScript is case sensitive. For example, a function parseInt is not same as the function Parseint.

  24. What boolean operators can be used in JavaScript?
    The ‘And’ Operator (&&), ‘Or’ Operator (||) and the ‘Not’ Operator (!) can be used in JavaScript.
    *Operators are without the parenthesis.

  25. What is the role of break and continue statements?
    Break statement is used to come out of the current loop while the continue statement continues the
    current loop with a new recurrence.

  26. How are DOM utilized in JavaScript?
    DOM stands for Document Object Model and is responsible for how various objects in a document
    interact with each other. DOM is required for developing web pages, which includes objects like
    paragraph, links, etc. These objects can be operated to include actions like add or delete. DOM is also
    required to add extra capabilities to a web page. On top of that, the use of API gives an advantage over
    other existing models.

  27. Explain the unshift() method ?
    This method is functional at the starting of the array, unlike the push(). It adds the desired number of
    elements to the top of an array.

  28. How to use external JavaScript file?
    I am assuming that js file name is message.js, place the following script tag inside the head tag.
    <script type=”text/javascript” src=”message.js”></script>

  29. What is DOM? What is the use of document object?
    DOM stands for Document Object Model. A document object represent the html document. It can be
    used to access and change the content of html.

  30. What is the use of window object?
    The window object is automatically created by the browser that represents a window of a browser.
    It is used to display the popup dialog box such as alert dialog box, confirm dialog box, input dialog box

  31. What is the use of history object?
    The history object of browser can be used to switch to history pages such as back and forward from
    current page or another page. There are three methods of history object.
    go(number): number may be positive for forward, negative for backward.

  32. How to write comment in JavaScript?
    There are two types of comments in JavaScript.
    Single Line Comment: It is represented by // (double forward slash)
    Multi Line Comment: It is represented by slash with asterisk symbol as /* write comment here */

  33. What is the difference between == and ===?
    The == operator checks equality only whereas === checks equality and data type i.e. value must be of
    same type.

  34. How to write html code dynamically using JavaScript?
    The innerHTML property is used to write the HTML code using JavaScript dynamically. Let’s see a simple
    document.getElementById(‘mylocation’).innerHTML=”<h2>This is heading using JavaScript</h2>”;

  35. How to write normal text code using JavaScript dynamically?
    The innerText property is used to write the simple text using JavaScript dynamically. Let’s see a simple
    document.getElementById(‘mylocation’).innerText=”This is text using JavaScript”;

  1. How to create objects in JavaScript?
    There are 3 ways to create object in JavaScript.
    By object literal
    By creating instance of Object
    By Object Constructor
    Let’s see a simple code to create object using object literal.
    emp={id:102,name:”Rahul Kumar”,salary:50000}

  2. What does the isNaN() function?
    The isNaN() function returns true if the variable value is not a number.

  3. What is the output of 10+20+”30″ in JavaScript?
    3030 because 10+20 will be 30. If there is numeric value before and after +, it is treated is binary +
    (arithmetic operator).

  4. What is the output of “10”+20+30 in JavaScript?
    102030 because after a string all the + will be treated as string concatenation operator (not binary +).

  5. Difference between Client side JavaScript and Server side JavaScript?
    Client side JavaScript comprises the basic language and predefined objects which are relevant to running
    java script in a browser. The client side JavaScript is embedded directly by in the HTML pages. This script
    is interpreted by the browser at run time.
    Server side JavaScript also resembles like client side java script. It has relevant java script which is to run
    in a server. The server side JavaScript are deployed only after compilation.

  6. What is the real name of JavaScript?
    The original name was Mocha, a name chosen by Marc Andreessen, founder of Netscape. In September
    of 1995, the name was changed to LiveScript. In December 1995, after receiving a trademark license
    from Sun, the name JavaScript was adopted.

  7. What is the difference between undefined value and null value?
    Undefined value: A value that is not defined and has no keyword is known as undefined value. For
    var number;//Here, number has undefined value.
    Null value: A value that is explicitly specified by the keyword “null” is known as null value. For example:
    var str=null;//Here, str has a null value.

  8. How to submit a form using JavaScript by clicking a link?
    Let’s see the JavaScript code to submit form on clicking the link.
    <form name=”myform” action=”index.php”>
    Search: <input type=’text’ name=’query’ />
    <a href=”javascript: submitform()”>Search</a>
    <script type=”text/javascript”>
    function submitform()

  9. How to change the background color of HTML document using JavaScript?
    <script type=”text/javascript”>

  10. How can you read properties of an Object in JavaScript?
    You can write and read properties of an object using the dot notation as follows −
    Getting object properties // ==> Zara
    emp.age // ==> 10
    Setting object properties = “Daisy” // <== Daisy
    emp.age = 20 // <== 20

  11. How can you create an Array in JavaScript?
    You can define arrays using the array literal as follows −
    var x = [];
    var y = [1, 2, 3, 4, 5];

  12. How to read elements of an array in JavaScript?
    An array has a length property that is useful for iteration. We can read elements of an array as follows −
    var x = [1, 2, 3, 4, 5];
    for (var i = 0; i < x.length; i++) {
    // Do something with x[i]

  13. What is a named function in JavaScript? How to define a named function?
    A named function has a name when it is defined. A named function can be defined using function
    keyword as follows −
    function named(){
    // do some stuff here

  14. How to define a anonymous function?
    An anonymous function can be defined in similar way as a normal function but it would not have any

  15. Can you assign a anonymous function to a variable?
    Yes! An anonymous function can be assigned to a variable.

  16. What is the purpose of ‘this’ operator in JavaScript?
    JavaScript famous keyword this always refers to the current context.

  17. What are the valid scopes of a variable in JavaScript?
    The scope of a variable is the region of your program in which it is defined. JavaScript variable will have
    only two scopes.
    Global Variables − A global variable has global scope which means it is visible everywhere in your
    JavaScript code.
    Local Variables − A local variable will be visible only within a function where it is defined. Function
    parameters are always local to that function.

  18. What is callback?
    A callback is a plain JavaScript function passed to some method as an argument or option. Some
    callbacks are just events, called to give the user a chance to react when a certain state is triggered.

  19. Which built-in method returns the character at the specified index?
    charAt() method returns the character at the specified index.

  20. How typeof operator works?
    The typeof is a unary operator that is placed before its single operand, which can be of any type. Its
    value is a string indicating the data type of the operand.
    The typeof operator evaluates to “number”, “string”, or “boolean” if its operand is a number, string, or
    boolean value and returns true or false based on the evaluation.

  21. How to redirect a url using JavaScript?
    This is very simple to do a page redirect using JavaScript at client side. To redirect your site visitors to a
    new page, you just need to add a line in your head section as follows −
    <script type=”text/javascript”>

%d bloggers like this: