Home >>Interview Questions >JavaScript Interview Questions
JavaScript can be complicated sometimes, in order to simplify the learning approach here are the top 60 JavaScript Interview Questions and Answers. Here are the frequently asked interview questions in the interview of JavaScript for professionals as well as beginners:
Answer :JavaScript is basically a scripting language that is different from the Java language. JavaScript is object-based, lightweight, and cross-platform translated language. It is most frequently used for client-side validation. For a translation of the JavaScript code for the web browser, the JavaScript Translator is responsible that is embedded in the browser.
Answer :There are many features of the JavaScript language, here are some of the important ones:
Here are some of the advantages of JavaScript:
Answer:Here are some of the dis-advantages of JavaScript:
Answer :The function that has been named at the time of definition is known as a named function.
Let's take an example to understand this:
function msg() { document.writeln("Named Function"); } msg();
Answer:Here are the two types of function in JavaScript:
function display() { document.writeln("Named Function"); } display();
var display=function() { document.writeln("Anonymous Function"); } display();
Answer:An anonymous function doesn't have any name. These types of functions are declared dynamically at the runtime with the help of function operator instead of the function declaration. The reason of using function operator is that it is more flexible than a function declaration. It can be easily replace an expression. Here is an example:
var display=function() { alert("Anonymous Function is invoked"); } display();
Answer:Indeed, you can assign an anonymous function to a variable.
Answer:An argument in JavaScript are passed to a function by the variables.
Answer:In JavaScript, when a variable is defined outside the scope in reference, is accessed from some inner scope, then we need closures.
Here is an example:
var num = 10; function sum() { document.writeln(num+num); } sum();
Answer:To find out a char value present at the specified index, the JavaScript string charAt() method is used. In this method, index number starts from 0 and goes to n-1, where n is the length of the string. Please note that the index value can't be a negative, greater than or equal to the length of the string.
Here is an example:
var str="Javatpoint"; document.writeln(str.charAt(4));
Answer:The JavaScript language has been provided by Netscape. In order to avoid the trademark issue, Microsoft changed the name and called it Jscript. In other words, JScript is the same as JavaScript, but Microsoft provides it.
Answer:Here is an example of JavaScript hello world is given below. Please note that you need to place it inside the body tag of HTML.
<script type="text/javascript"> document.write("JavaScript Hello World!"); </script>
Answer:Let's assume that js file name is message.js, enter the following script tag inside the head tag:
<script type="text/javascript" src="message.js"></script>
Answer:Indeed, JavaScript is a case sensitive language. Here is an example:
Var msg = "JavaScript is a case-sensitive language"; //Here, var should be used to declare a variable function display() { document.writeln(msg); // It will not display the result. } display();
Answer:The full form of the abbreviation ‘BOM’ is Browser Object Model. It is known to provide interaction with the browser. Window is the default object of a browser hence, you can call all the functions of the window either by specifying the window or directly. Various properties are provided by the window object like document, history, screen, navigator, location, innerHeight, innerWidth,
Answer:The full form of the abbreviation ‘DOM’ is document object model. It represents the HTML document and can be used to access and modify the content of HTML.
Answer:The window object is generated automatically by the browser and represents the window of a browser. Please note that it is not an object of JavaScript instead it is a browser object. It is also used to display the popup dialog box.
Here is a brief description:
Method | Description |
---|---|
alert() | This method displays the alert box having the message with ok button. |
confirm() | This method displays the confirm dialog box having the message with ok and cancel button. |
prompt() | This method displays a dialog box to get input from the user. |
prompt() | This method displays a dialog box to get input from the user. |
open() | This method opens the new window. |
close() | This method closes the current window. |
setTimeout() | This method performs the action after specified time like calling function, evaluating expressions. |
Answer:You can use the history object of a browser to switch to history pages such as back and forward from the current page or another page. There are basically three methods of history object:
Answer:There are generally two types of comments in JavaScript:
Answer: In order to create a function in JavaScript please follow the below mentioned syntax:
function function_name() { //function body }
Answer:Generally, there are two types of datatypes in JavaScript:
a. Primitive data types: here are some of the primitive data types which are as follows:
Data Type | Description |
---|---|
String | This data type represents a sequence of characters, e.g., "hi" |
Number | This data type represents numeric values, e.g., 500 |
Boolean | This data type represents boolean value either false or true |
Undefined | This data type represents an undefined value |
Null | |
This data type represents null, i.e., no value at all |
b.Non-Primitive data types:Here are some of the non-primitive data types which are as follows:
Data Type | Description |
---|---|
Object | This data type represents an instance through which members can be accessed. |
Array | This data type represents a group of similar values |
RegExp | This data type represents regular expression |
Answer:The ‘==’ operator in JavaScript checks the equality and on the other hand ‘===’ operator checks the equality and the data type, i.e., a value must be of the same type.
Answer:In order to write the HTML code dynamically using JavaScript, InnerHTML property is used. Have a look at the example here to understand it more clearly:
document.getElementById('mylocation').innerHTML="<h2>This is heading using JavaScript</h2>";
Answer:In order to write the normal text code dynamically using JavaScript, InnerHTML property is used. Have a look at the example here to understand it more clearly:
document.getElementById('mylocation').innerText="This is text using JavaScript";
Answer:Generally, there are three ways by which Objects can be created in JavaScript:
Here is a simple code to create an object using the object literal:
emp={id:105,name:"Sonu kumar",salary:150000}
Answer:You can create an array in JavaScript using these three ways:
Here is a simple code to create an array in JavaScript:
var emp=["Sonu","mithun","PHPTPOINT"];
Answer:Whenever the value of a variable is not a number, the isNaN() function returns true. Here is an example:
<script> function number(num) { if (isNaN(num)) { return "Not a Number"; } return "Number"; } console.log(number('1000F')); // expected output: "Not a Number" console.log(number('1000')); // expected output: "Number" </script>
The output of the code 10+20+"30" in JavaScript is 3030 as 10+20 will be 30. This is because, if there is any numeric value pre or post of + then it treats it as binary + (arithmetic operator).
Here is the code;
<script> function display() { document.writeln(10+20+"30"); } display(); </script>
The output of "10"+20+30 in JavaScript is 102030. This is because after a string all the + will be treated as string concatenation operator instead of (not binary +).
Answer:Client-side JavaScript - this basically constitutes the basic language and predefined objects that are relevant to running JavaScript in a browser. It is embedded directly in the HTML pages. This script is interpreted by the browser at runtime.
Server-side JavaScript - This resembles the client-side JavaScript. Server-side JavaScript has a relevant JavaScript which is to run in a server. Please note that only after compilation server-side JavaScript is deployed.
Answer:It depends on the OS and the browser that where should be the storage of the cookies. Cookies.txt file that contains all the cookies is used on the Netscape Navigator on Windows. Here is the path c:\Program Files\Netscape\Users\username\cookies.txt Username@website.txt is the name of the file where the Internet Explorer stores the cookies. Here is the path: c:\Windows\Cookies\username@Website.txt.
Marc Andreessen, founder of Netscape had chosen the original name of JavaScript i.e. Mocha. The name was changed to LiveScript in the September of 1995. After receiving a trademark license from Sun in the December of 1995, the name JavaScript was adopted.
Answer:Undefined value : A value without a keyword and that has not been defined is called as undefined value.
Here is an example:
int number;//Here, a number has an undefined value.
Null Value:This is the value that is specified by the keyword “null”. Here is an example:
String str=null;//Here, str has a null value
Answer:With the help of property named as "cursor", you can set the cursor to wait in JavaScript. Here is an example depicting the usage:
<script> window.document.body.style.cursor = "wait"; </script>