JavaScript Interview Questions
JavaScript Interview Questions
o Lightweight
o Interpreted programming language
o Good for the applications which are network-centric
o Complementary to Java
o Complementary to HTML
o Open source
Cross-platform
1. function msg()
2. {
3. document.writeln("Named Function");
4. }
5. msg();
o Named - These type of functions contains name at the time of definition. For
Example:
1. function display()
2. {
3. document.writeln("Named Function");
4. }
5. display();
o Anonymous - These type of functions doesn't contain any name. They are declared
dynamically at runtime.
1. var display=function()
2. {
3. document.writeln("Anonymous Function");
4. }
5. display();
1. var display=function()
2. {
3. alert("Anonymous Function is invoked");
4. }
5. display();
1. var str="Javatpoint";
2. document.writeln(str.charAt(4));
1. <script type="text/javascript">
2. document.write("JavaScript Hello World!");
3. </script>
Java JavaScript
Java creates applications that can run in any JavaScript code can run only in the browser, but it
virtual machine (JVM) or browser. can now run on the
server via Node.js.
The Java code needs to be compiled. The JavaScript code doesn't require to be complied.
Java Objects are class-based. You can't make any JavaScript Objects are prototype-based.
program in Java without creating a class.
Java is a Complete and Standalone language that JavaScript is assigned within a web page and
can be used in backend coding. integrates with its
HTML content.
Java programs consume more memory. JavaScript code is used in HTML web pages and
requires less memory.
The file extension of the Java program is written The JavaScript file extension is written as ".js" and it
as ".Java" and it translates source code into is interpreted
bytecodes which are then executed by JVM (Java but not compiled. Every browser has a JavaScript
Virtual Machine). interpreter to e
xecute the JS code.
15) What are the key differences between Java and JavaScript? /
How is JavaScript different from Java?
JavaScript is a lightweight programming language (most commonly known as scripting
language) developed by Netscape, Inc. It is used to make web pages interactive. It is not
a part of the Java platform. Following is a list of some key differences between Java and
JavaScript
Method Description
alert() displays the alert box containing the message with ok button.
confirm() displays the confirm dialog box containing the message with ok and cancel button.
prompt() displays a dialog box to get input from the user.
setTimeout() performs the action after specified time like calling function, evaluating expressions.
1. function function_name(){
2. //function body
3. }
More details.
String: The string data type represents a sequence of characters. It is written within
quotes and can be represented using a single or a double quote.
Example:
Number: The number data type is used to represent numeric values and can be written
with or without decimals.
Example:
Boolean: The Boolean data type is used to represent a Boolean value, either false or true.
This data type is generally used for conditional testing.
Example:
1. var x = 5;
2. var y = 6;
3. var z = 5;
4. (x == y) // returns false
5. (x == z) //returns true
BigInt: The BigInt data type is used to store numbers beyond the Number data type
limitation. This data type can store large integers and is represented by adding "n" to an
integer literal.
Example:
Undefined: The Undefined data type is used when a variable is declared but not assigned.
The value of this data type is undefined, and its type is also undefined.
Example:
Null: The Null data type is used to represent a non-existent, null, or a invalid value i.e. no
value at all.
Example:
1. var x = null;
Symbol: Symbol is a new data type introduced in the ES6 version of JavaScript. It is used
to store an anonymous and unique value.
Example:
typeof: The typeof operator is used to determine what type of data a variable or operand
contains. It can be used with or without parentheses (typeof(x) or typeof x). This is mainly
used in situations when you need to process the values of different types.
Example:
1. typeof 10; // Returns: "number"
2. typeof 10.0; // Returns: "number"
3. typeof 2.5e-4; // Returns: "number"
4. typeof Infinity; // Returns: "number"
5. typeof NaN; // Returns: "number". Despite being "Not-A-Number"
6. // Strings
7. typeof ''; // Returns: "string"
8. typeof 'Welcome to JavaTpoint'; // Returns: "string"
9. typeof '12'; // Returns: "string". Number within quotes is typeof string
10. // Booleans
11. typeof true; // Returns: "boolean"
12. typeof false; // Returns: "boolean"
13. // Undefined
14. typeof undefined; // Returns: "undefined"
15. typeof undeclaredVariable; // Returns: "undefined"
16. // Null
17. typeof Null; // Returns: "object"
18. // Objects
19. typeof {name: "John", age: 18}; // Returns: "object"
20. // Arrays
21. typeof [1, 2, 3]; // Returns: "object"
22. // Functions
23. typeof function(){}; // Returns: "function"
In the above examples, we can see that the primitive data types can store only a single
value. To store multiple and complex values, we have to use non-primitive data types.
Object: The Object is a non-primitive data type. It is used to store collections of data. An
object contains properties, defined as a key-value pair. A property key (name) is always a
string, but the value can be any data type, such as strings, numbers, Booleans, or complex
data types like arrays, functions, and other objects.
Example:
1. // Collection of data in key-value pairs
2. var obj1 = {
3. x: 123,
4. y: "Welcome to JavaTpoint",
5. z: function(){
6. return this.x;
7. }
8. }
Array: The Array data type is used to represent a group of similar values. Every value in
an array has a numeric position, called its index, and it may contain data of any data type-
numbers, strings, Booleans, functions, objects, and even other arrays. The array index
starts from 0 so that the first array element is arr[0], not arr[1].
Example:
More details.
1. By object literal
2. By creating an instance of Object
3. By Object Constructor
1. emp={id:102,name:"Rahul Kumar",salary:50000}
More details.
1. By array literal
2. By creating an instance of Array
3. By using an Array constructor
1. var emp=["Shyam","Vimal","Ratan"];
More details.
30) What does the isNaN() function?
The isNan() function returns true if the variable value is not a number. For example:
1. function number(num) {
2. if (isNaN(num)) {
3. return "Not a Number";
4. }
5. return "Number";
6. }
7. console.log(number('1000F'));
8. // expected output: "Not a Number"
9.
10. console.log(number('1000'));
11. // expected output: "Number"
1. function display()
2. {
3. document.writeln(10+20+"30");
4. }
5. display();
1. function display()
2. {
3. document.writeln("10"+20+30);
4. }
5. display();
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.
For example: If you use it in a form element, it prevents it from submitting. If used in an
anchor element, it prevents it from navigating. If used in a contextmenu, it prevents it
from showing or displaying.
On the other hand, the event.stopPropagation() method is used to stop the propagation
of an event or stop the event from occurring in the bubbling or capturing phase.
38) 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:
Null value: A value that is explicitly specified by the keyword "null" is known as a null
value. For example:
1. <script>
2. window.document.body.style.cursor = "wait";
3. </script>
1. var num=-5;
2. function display()
3. {
4. document.writeln(num/0);
5. }
6. display();
7. //expected output: -Infinity
43) 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.
44) What are the pop-up boxes available in JavaScript?
o Alert Box
o Confirm Box
o Prompt Box
1. <script type="text/javascript">
2. function msg(){
3. alert("Hello Alert Box");
4. }
5. </script>
6. <input type="button" value="click" onclick="msg()"/>
1. <script type="text/javascript">
2. function msg(){
3. var v= confirm("Are u sure?");
4. if(v==true){
5. alert("ok");
6. }
7. else{
8. alert("cancel");
9. }
10.
11. }
12. </script>
13.
14. <input type="button" value="delete record" onclick="msg()"/>
1. <script type="text/javascript">
2. function msg(){
3. var v= prompt("Who are you?");
4. alert("I am "+v);
5.
6. }
7. </script>
8.
9. <input type="button" value="click" onclick="msg()"/>
1. <script type="text/javascript">
2. document.body.bgColor="pink";
3. </script>
1. <script>
2. function validateform(){
3. var name=document.myform.name.value;
4. var password=document.myform.password.value;
5.
6. if (name==null || name==""){
7. alert("Name can't be blank");
8. return false;
9. }else if(password.length<6){
10. alert("Password must be at least 6 characters long.");
11. return false;
12. }
13. }
14. </script>
15. <body>
16. <form name="myform" method="post" action="abc.jsp" onsubmit="return valid
ateform()" >
17. Name: <input type="text" name="name"><br/>
18. Password: <input type="password" name="password"><br/>
19. <input type="submit" value="register">
20. </form>
Test it Now
1. <script>
2. function validateemail()
3. {
4. var x=document.myform.email.value;
5. var atposition=x.indexOf("@");
6. var dotposition=x.lastIndexOf(".");
7. if (atposition<1 || dotposition<atposition+2 || dotposition+2>=x.length){
8. alert("Please enter a valid e-
mail address \n atpostion:"+atposition+"\n dotposition:"+dotposition);
9. return false;
10. }
11. }
12. </script>
13. <body>
14. <form name="myform" method="post" action="#" onsubmit="return validatee
mail();">
15. Email: <input type="text" name="email"><br/>
16.
17. <input type="submit" value="register">
18. </form>
Test it Now
1. var address=
2. {
3. company:"Javatpoint",
4. city:"Noida",
5. state:"UP",
6. fullAddress:function()
7. {
8. return this.company+" "+this.city+" "+this.state;
9. }
10. };
11. var fetch=address.fullAddress();
12. document.writeln(fetch);
1. function display()
2. {
3. x = 10;
4. y = 15;
5. z = x + y;
6. debugger;
7. document.write(z);
8. document.write(a);
9. }
10. display();
1. "use strict";
2. x=10;
3. console.log(x);
1. function display()
2. {
3. document.writeln(Math.random());
4. }
5. display();
1. function display()
2. {
3. var x=102;//integer value
4. var y=102.7;//floating point value
5. var z=13e4;//exponent value, output: 130000
6. var n=new Number(16);//integer value by number object
7. document.write(x+" "+y+" "+z+" "+n);
8. }
9. display();
1. function display()
2. {
3. document.writeln(10<20);//true
4. document.writeln(10<5);//false
5. }
6. display();
1. function display()
2. {
3. var arr1= [1,2,3,4,5,6,7,8,9,10];
4. arr1.copyWithin(2) ;
5. document.write(arr1);
6. }
7. display();
1. function display()
2. {
3. var set = new Set();
4. set.add("jQuery");
5. set.add("AngularJS");
6. set.add("Bootstrap");
7. for (let elements of set) {
8. document.writeln(elements+"<br>");
9. }
10. }
11. display();
63) What is the use of a WeakSet object in JavaScript?
The JavaScript WeakSet object is the type of collection that allows us to store weakly held
objects. Unlike Set, the WeakSet are the collections of objects only. It doesn't contain the
arbitrary values. For example:
1. function display()
2. {
3. var ws = new WeakSet();
4. var obj1={};
5. var obj2={};
6. ws.add(obj1);
7. ws.add(obj2);
8. //Let's check whether the WeakSet object contains the added object
9. document.writeln(ws.has(obj1)+"<br>");
10. document.writeln(ws.has(obj2));
11. }
12. display()
1. function display()
2. {
3. var map=new Map();
4. map.set(1,"jQuery");
5. map.set(2,"AngularJS");
6. map.set(3,"Bootstrap");
7. document.writeln(map.get(1)+"<br>");
8. document.writeln(map.get(2)+"<br>");
9. document.writeln(map.get(3));
10. }
11. display();
1. function display()
2. {
3. var wm = new WeakMap();
4. var obj1 = {};
5. var obj2 = {};
6. var obj3= {};
7. wm.set(obj1, "jQuery");
8. wm.set(obj2, "AngularJS");
9. wm.set(obj3,"Bootstrap");
10. document.writeln(wm.has(obj2));
11. }
12. display();
66) What are the falsy values in JavaScript, and how can we check
if a value is falsy?
Those values which become false while converting to Boolean are called falsy values.
We can check if a value is falsy by using the Boolean function or the Double NOT operator
(!!).
Example 1:
1. hoistedVariable = 12;
2. console.log(hoistedVariable); // outputs 12 even when the variable is declared aft
er it is initialized
3. var hoistedVariable;
Example2:
1. hoistedFunction(); // Outputs " Welcome to JavaTpoint " even when the function
is declared after calling
2. function hoistedFunction(){
3. console.log(" Welcome to JavaTpoint ");
4. }
5. Example3:
6. // Hoisting in a local scope
7. function doSomething(){
8. x = 11;
9. console.log(x);
10. var x;
11. }
12. doSomething(); // Outputs 11 since the local variable "x" is hoisted inside the loca
l scope