JavaScript Interview Questions
What is JavaScript
JavaScript is a scripting language. It is different from Java language. It is object-based, lightweight, cross-platform translated language. It is widely used for client-side validation. The JavaScript Translator (embedded in the browser) is responsible for translating the JavaScript code for the web browser.
List some features of JavaScript
Some of the features of JavaScript are:
- Lightweight
- Interpreted programming language
- Good for the applications which are network-centric
- Complementary to Java
- Complementary to HTML
- Open source
Cross-platform
List some of the advantages of JavaScript
Some of the advantages of JavaScript are:
- Server interaction is less
- Feedback to the visitors is immediate
- Interactivity is high
- Interfaces are richer
List some of the disadvantages of JavaScript
This is a javascript technical interview questions which asked frequently in realtime.
Some of the disadvantages of JavaScript are:
- No support for multithreading
- No support for multiprocessing
- Reading and writing of files is not allowed
No support for networking applications.
Define a named function in JavaScript.
The function which has named at the time of definition is called a named function. For example
function msg() Â
{Â Â
  document.writeln(“Named Function”); Â
}Â Â
msg();Â Â
Name the types of functions
The types of function are:
- Named – These type of functions contains name at the time of definition. For Example:
function display() Â
{Â Â
  document.writeln(“Named Function”); Â
}Â Â
display();Â Â
- Anonymous – These type of functions doesn’t contain any name. They are declared dynamically at runtime.
var display=function() Â
{Â Â
  document.writeln(“Anonymous Function”); Â
}Â Â
display();Â Â
Define anonymous function
It is a function that has no name. These functions are declared dynamically at runtime using the function operator instead of the function declaration. The function operator is more flexible than a function declaration. It can be easily used in the place of an expression. For example:
var display=function() Â
{Â Â
  alert(“Anonymous Function is invoked”); Â
}Â Â
display();Â
Can an anonymous function be assigned to a variable?
Yes, you can assign an anonymous function to a variable.
In JavaScript what is an argument object?
The variables of JavaScript represent the arguments that are passed to a function.
Define closure.
In JavaScript, we need closures when a variable which is defined outside the scope in reference is accessed from some inner scope.
var num = 10; Â
function sum()  Â
{Â Â
document.writeln(num+num);Â Â
}Â Â Â
sum();Â Â
If we want to return the character from a specific index which method is used?
The JavaScript string charAt() method is used to find out a char value present at the specified index. The index number starts from 0 and goes to n-1, where n is the length of the string. The index value can’t be a negative, greater than or equal to the length of the string. For example:
var str=”Javatpoint”;   Â
document.writeln(str.charAt(4));Â Â Â Â
What is the difference between JavaScript and JScript?
Netscape provided the JavaScript language. Microsoft changed the name and called it JScript to avoid the trademark issue. In other words, you can say JScript is the same as JavaScript, but Microsoft provides it.
How to write a hello world example of JavaScript?
A simple example of JavaScript hello world is given below. You need to place it inside the body tag of HTML.
<script type=”text/javascript”> Â
document.write(“JavaScript Hello World!”); Â
</script>Â Â
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>
Is JavaScript case sensitive language?
By this javascript interview questions we prepare well for the interview and it is for fresher and as well as for experience people.
Yes, JavaScript is a case sensitive language. For 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();Â Â
What is DOM? What is the use of document object?
DOM stands for Document Object Model. A document object represents the HTML document. It can be used to access and change the content of HTML.
What is the use of window object?
The window object is created automatically by the browser that represents a window of a browser. It is not an object of JavaScript. It is a browser object.
The window object is used to display the popup dialog box. Let’s see with description.
No. | Method | Description |
---|---|---|
1 | alert() | Displays the alert box containing the message with ok button. |
2 | confirm() | Displays the confirm dialog box containing the message with ok and cancel button. |
3 | prompt() | Displays a dialog box to get input from the user. |
4 | open() | Opens the new window. |
5 | close() | Closes the current window. |
5 | setTimeout() | Performs the action after specified time like calling function, evaluating expressions. |
Comment in JavaScript frequent asked javascript interview questions?
There are two types of comments in JavaScript.
- Single Line Comment: It is represented by // (double forward slash)
- Multi-Line Comment: Slash represents it with asterisk symbol as /* write comment here */
How to create a function in JavaScript?
To create a function in JavaScript, follow the following syntax.
function function_name(){ Â
//function body Â
}Â Â
What are the JavaScript data types?
There are two types of data types in JavaScript:
- Primitive Data Types – The primitive data types are as follows:
Data Type | Description |
---|---|
String | represents a sequence of characters, e.g., "hello" |
Number | represents numeric values, e.g., 100 |
Boolean | represents boolean value either false or true |
Undefined | represents an undefined value |
Null | represents null, i.e., no value at all |
Non-primitive Data Types – The non-primitive data types are as follows:
Data Type | Description |
---|---|
Object | represents an instance through which we can access members |
Array | represents a group of similar values |
RegExp | represents regular expression |
What is the difference between == and ===?
The == operator checks equality only whereas === checks equality, and data type, i.e., a value must be of the same type.
How to write HTML code dynamically using JavaScript?
The inner HTML property is used to write the HTML code using JavaScript dynamically. Let’s see a simple example:
document.getElementById(‘mylocation’).innerHTML=”<h2>This is heading using JavaScript</h2>”;Â
How to create objects in JavaScript?
There are 3 ways to create an object in JavaScript.
- By object literal
- By creating an instance of Object
- By Object Constructor
Let’s see a simple code to create an object using object literal.
emp={id:102,name:”Rahul Kumar”,salary:50000}
Advanced javascript interview questions
How to create an array in JavaScript?
There are 3 ways to create an array in JavaScript.
- By array literal
- By creating an instance of Array
- By using an Array constructor
Let’s see a simple code to create an array using object literal.
var emp=[“Shyam”,”Vimal”,”Ratan”];   Â
What does the isNaN() function?
This javascript interview questions and answers for freshers regularly asked in the walk-in interview.
The isNan() function returns true if the variable value is not a number. For example:
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”Â
What is the output of 10+20+"30" in JavaScript?
javascript interview questions for 2 years experience people asked 3030 because 10+20 will be 30. If there is numeric value before and after +, it treats as binary + (arithmetic operator).
function display() Â
{Â Â
  document.writeln(10+20+”30″); Â
}Â Â
display();
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 +).
function display() Â
{Â Â
  document.writeln(“10″+20+30); Â
}Â Â
display();Â Â
Difference between Client side JavaScript and Server side JavaScript?
Client-side JavaScript comprises the basic language and predefined objects which are relevant to running JavaScript in a browser. The client-side JavaScript is embedded directly by in the HTML pages. The browser interprets this script at runtime.
Server-side JavaScript also resembles client-side JavaScript. It has a relevant JavaScript which is to run in a server. The server-side JavaScript are deployed only after compilation.
In which location cookies are stored on the hard disk?
The storage of cookies on the hard disk depends on the OS and the browser.
The Netscape Navigator on Windows uses a cookies.txt file that contains all the cookies. The path is c:\Program Files\Netscape\Users\username\cookies.txt
The Internet Explorer stores the cookies on a file username@website.txt. The path is: c:\Windows\Cookies\username@Website.txt.
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.
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 example:
- int number;//Here, a number has an undefined value. Â
Null value:Â A value that is explicitly specified by the keyword “null” is known as a null value. For example:
String str=null;//Here, str has a null value.Â
How to set the cursor to wait in JavaScript?
The cursor can be set to wait in JavaScript by using the property “cursor”. The following example illustrates the usage:
<script>Â Â
window.document.body.style.cursor = “wait”;  Â
</script>Â Â
36) What is this [[[]]]?
This is a three-dimensional array.
var myArray = [[[]]]; Â
What is this [[[]]]?
This is a three-dimensional array.
var myArray = [[[]]];Â
Are Java and JavaScript same?
No, Java and JavaScript are the two different languages. Java is a robust, secured and object-oriented programming language whereas JavaScript is a client-side scripting language with some limitations.
What is negative infinity?
Negative Infinity is a number in JavaScript which can be derived by dividing the negative number by zero. For example:
var num=-5; Â
function display() Â
{Â Â
  document.writeln(num/0); Â
}Â Â
display();Â Â
//expected output: -Infinity Â
What is the difference between View state and Session state?
“View state” is specific to a page in a session whereas “Session state” is specific to a user or browser that can be accessed across all pages in the web application.
What are the pop-up boxes available in JavaScript?
- Alert Box
- Confirm Box
- Prompt Box
Example of alert() in JavaScript
<script type=”text/javascript”> Â
function msg(){ Â
 alert(“Hello Alert Box”); Â
}Â Â
</script>Â Â
<input type=”button” value=”click” onclick=”msg()”/> Â
Example of confirm() in JavaScript
<script type=”text/javascript”> Â
function msg(){ Â
var v= confirm(“Are u sure?”); Â
if(v==true){Â Â
alert(“ok”);Â Â
}Â Â
else{Â Â
alert(“cancel”);Â Â
}Â Â
}Â Â
</script>Â Â
 <input type=”button” value=”delete record” onclick=”msg()”/> Â
Example of prompt() in JavaScript
<script type=”text/javascript”> Â
function msg(){ Â
var v= prompt(“Who are you?”); Â
alert(“I am “+v); Â
 } Â
</script>Â Â
  <input type=”button” value=”click” onclick=”msg()”/> Â
How can we detect OS of the client machine using JavaScript?
The navigator.appVersion string can be used to detect the operating system on the client machine.
How to submit a form using JavaScript by clicking a link?
Let’s see the JavaScript code to submit the form by clicking the link.
<form name=”myform” action=”index.php”> Â
Search: <input type=’text’ name=’query’ /> Â
<a href=”javascript: submitform()”>Search</a> Â
</form>Â Â
<script type=”text/javascript”> Â
function submitform() Â
{Â Â
  document.myform.submit(); Â
}Â Â
</script>
Is JavaScript faster than ASP script?
Yes, because it doesn’t require web server’s support for execution.
How to change the background color of HTML document using JavaScript?
<script type=”text/javascript”> Â
document.body.bgColor=”pink”;Â Â
</script>Â Â
How to handle exceptions in JavaScript?
By the help of try/catch block, we can handle exceptions in JavaScript. JavaScript supports try, catch, finally and throw keywords for exception handling.
How to validate a form in JavaScript?
<script>Â Â
function validateform(){ Â
var name=document.myform.name.value; Â
var password=document.myform.password.value; Â
 Â
if (name==null || name==””){ Â
  alert(“Name can’t be blank”); Â
  return false; Â
}else if(password.length<6){ Â
  alert(“Password must be at least 6 characters long.”); Â
  return false; Â
  } Â
}Â Â
</script>Â Â
<body>Â Â
<form name=”myform” method=”post” action=”abc.jsp” onsubmit=”return validateform()” > Â
Name: <input type=”text” name=”name”><br/> Â
Password: <input type=”password” name=”password”><br/> Â
<input type=”submit” value=”register”> Â
</form>Â Â
How to validate email in JavaScript?
<script>Â Â
function validateemail() Â
{Â Â
var x=document.myform.email.value; Â
var atposition=x.indexOf(“@”); Â
var dotposition=x.lastIndexOf(“.”); Â
if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){ Â
  alert(“Please enter a valid e-mail address \n atpostion:”+atposition+”\n dotposition:”+dotposition); Â
  return false; Â
  } Â
}Â Â
</script>Â Â
<body>Â Â
<form name=”myform”  method=”post” action=”#” onsubmit=”return validateemail();”> Â
Email: <input type=”text” name=”email”><br/> Â
 Â
<input type=”submit” value=”register”> Â
</form>Â Â
What is this keyword in JavaScript?
The this keyword is a reference variable that refers to the current object. For example:
var address=   Â
{Â Â Â Â
company:”Javatpoint”,   Â
city:”Noida”,   Â
state:”UP”,   Â
fullAddress:function()Â Â Â Â
{Â Â Â Â
return this.company+” “+this.city+” “+this.state;   Â
}Â Â Â Â
};Â Â Â Â
var fetch=address.fullAddress();   Â
document.writeln(fetch);Â Â
What is the requirement of debugging in JavaScript?
JavaScript didn’t show any error message in a browser. However, these mistakes can affect the output. The best practice to find out the error is to debug the code. The code can be debugged easily by using web browsers like Google Chrome, Mozilla Firebox.
To perform debugging, we can use any of the following approaches:
- Using console.log() method
- Using debugger keyword
What is the use of Math object in JavaScript?
The JavaScript math object provides several constants and methods to perform a mathematical operation. Unlike date object, it doesn’t have constructors. For example:
function display() Â
{Â Â
  document.writeln(Math.random()); Â
}Â Â
display();Â
What is the use of a Date object in JavaScript?
The JavaScript date object can be used to get a year, month and day. You can display a timer on the webpage by the help of JavaScript date object.
function display() Â
{Â Â
  var date=new Date();   Â
var day=date.getDate();   Â
var month=date.getMonth()+1;   Â
var year=date.getFullYear();   Â
document.write(“<br>Date is: “+day+”/”+month+”/”+year);   Â
}Â Â
display();
What is the use of a Number object in JavaScript?
The JavaScript number object enables you to represent a numeric value. It may be integer or floating-point. JavaScript number object follows the IEEE standard to represent the floating-point numbers.
function display() Â
{Â Â
var x=102;//integer value   Â
var y=102.7;//floating point value   Â
var z=13e4;//exponent value, output: 130000   Â
var n=new Number(16);//integer value by number object   Â
document.write(x+” “+y+” “+z+” “+n);   Â
}Â Â Â Â Â
display();Â
What is the use of a Boolean object in JavaScript?
The JavaScript Boolean is an object that represents value in two states: true or false. You can create the JavaScript Boolean object by Boolean() constructor.
function display() Â
{Â Â
document.writeln(10<20);//true   Â
document.writeln(10<5);//false    Â
}Â Â Â Â Â
display();
if anyone intrest javascript interview questions pdf please mail us.
To know more about Java Script please clickÂ
Practice HTML online pls click html w3
To know more about HTML please Click Here