Hi guys, welcome back. In this module, I will be discussing the top 50 JavaScript Interview Questions and Answers. Destructuring in JavaScript was covered in the previous module. You can read our previous module if you want to know about it. So, let’s get started to know JavaScript Interview Questions and Answers.
List of 50 Most Asked JavaScript Interview Questions and Answers
1. Mention the different data types in JavaScript.
Primitive Data types
- Number
- Boolean
- String
- Undefined
- Symbol
- Null
Non-primitive data types
- Object
- Array
2. Define hoisting in JavaScript.
This is the second question of the top 50 JavaScript Interview Questions and Answers. Hoisting is Javascript default behaviour in which all variable and function declarations are moved to the top.
3. Mention the difference between ‘==’ & ‘===’.
This is the third question of the top 50 JavaScript Interview Questions and Answers. ‘==’ returns true if two values are same whereas ‘===’ returns true if the data type & value of a variable are the same.
var a = 3, b = '3'; console.log(a==b); console.log(a===b); // Output // true // false
4. Is JavaScript a dynamically typed language?
This is the 4th question of the top 50 JavaScript Interview Questions and Answers. Yes, because the data type of variables is decided at runtime, JavaScript is a dynamically typed language.
5. What is the usage of isNaN in JavaScript?
This is the 5th question of the top 50 JavaScript Interview Questions and Answers. NaN stands for “Not a Number”. It returns true if the given value is not a number, otherwise false.
console.log(isNaN(5.5)); console.log(isNaN('hello')); console.log(isNaN(true)); // Output // false // true // false
6. In JavaScript, what do you mean by “this” keyword?
This is the 6th question of the top 50 JavaScript Interview Questions and Answers. ‘this’ keyword in JavaScript refers to the object from where it was called. Depending on where it is used, the “this” has multiple values. It refers to the owner object in a method and the global object in a function.
7. How can the style of an HTML element be changed using JavaScript?
The style of a HTML element can be changed in 2 ways:
- setAttribute() method
element.setAttriute(‘style’, cssProperty);
For example,element.setAttribute("style", "background-color: yellow;");
- style property
element.style.cssProperty = value;
For example,element.style.background="yellow";
8. What would be the output of the JavaScript code given below?
console.log(5+3+'6'); // Output // 86
9. Mention the different types of pop-up boxes in JavaScript.
There are 3 types of pop-up boxes in
- alert()
For example,
alert(‘Alert Box’); - prompt()
For example,
const value = prompt(‘Enter a value:’);
console.log(value); - confirm()
For example,
const value = confirm(“Do you want to continue?”);
console.log(value);
10. Define undefined in JavaScript.
This is the 10th question of the top 50 JavaScript Interview Questions and Answers. In JavaScript, undefined signifies that a variable has been declared but not assigned a value or that it has not been declared at all.
11. Define null in JavaScript.
This is the 11th question of the top 50 JavaScript Interview Questions and Answers. Null in JavaScript represents the absence of any value. It can be assigned to a variable to indicate that it currently has no value but will do so in the future.
12. What is the usage of typeof operator in JavaScript?
This is the 12th question of the top 50 JavaScript Interview Questions and Answers. typeof operator is used for checking the data type of a variable or value.
const x = 5, y = 'Hello'; console.log(typeof x, typeof y); // Output // number string
13. What is DOM in JavaScript?
This is the 13th question of the top 50 JavaScript Interview Questions and Answers. DOM stands for Document Object Model. It is a hierarchical representation of an HTML document.
14. What are the methods for accessing & manipulating the document object model (DOM)?
- document.getElementById(idName)
- document.getElementsByClass(className)
- document.getElementsByTagName(TagName)
- document.querySelector(cssSelector)
- document.getElementsByName(name);
- document.querySelectorAll(cssSelector)
15. What is the difference between push & unshift methods of an array in JavaScript?
This is the 15th question of the top 50 JavaScript Interview Questions and Answers. push() method is an array method used for inserting an element at the end of an array.
For example,
const arr = [-4, 9, -7, 11, 19]; console.log(arr); arr.push(6); console.log(arr); // Output // [-4, 9, -7, 11, 19] // [-4, 9, -7, 11, 19, 6]
unshift() method is an array method used for inserting an element at the beginning of an array.
For example,
const arr = [-4, 9, -7, 11, 19]; console.log(arr); arr.unshift(6); console.log(arr); // Output // [-4, 9, -7, 11, 19] // [6, -4, 9, -7, 11, 19]
16. What is the use of for-in loop in JavaScript?
This is the 16th question of the top 50 JavaScript Interview Questions and Answers. The for-in loop in JavaScript is used for iterating through the properties of an object.
Syntax
for(let variableName in objectName){ // do something } For example, var person = { name: "Shikhar Gupta", age: 22, gender: "Male", email:"[email protected]" }; for(let key in person) { console.log(key, person[key]); } //Output //name Shikhar Gupta //age 22 //gender Male //email [email protected]
17. What is the difference between pop() & shift method of arrays in JavaScript?
This is the 17th question of the top 50 JavaScript Interview Questions and Answers. The pop() method is an array method used for deleting an element at the end of an array.
For example,
const arr = [-4, 9, -7, 11, 19]; console.log(arr); arr.pop(); console.log(arr); // Output // [-4, 9, -7, 11, 19] // [-4, 9, -7, 11]
shift() method is an array method used for deleting an element at the beginning of an array.
For example,
const arr = [-4, 9, -7, 11, 19]; console.log(arr); arr.shift(); console.log(arr); // Output // [-4, 9, -7, 11, 19] // [9, -7, 11, 19]
18. What is the difference between innerHTML & innerText in JavaScript?
This is the 18th question of the top 50 JavaScript Interview Questions and Answers. The innerText is an attribute used to retrieve and set the content of an HTML element or tag, the spaces are not taken into consideration. It is not possible to use HTML tags when using it; otherwise, plain text will be written.
The innerHTML attribute is used to print or change HTML and the text contained within them as well as to set the content of HTML components. The spaces are also included. HTML tags can also be inserted.
19. Name the method(s) used for printing the message on the HTML document.
- document.write()
For example,
document.write(‘Hello, World!’); - document.writeln()
For example,
document.writeln(‘Hello, World!’);
20. What is an arrow function?
This is the 20th question of the top 50 JavaScript Interview Questions and Answers. An arrow function is a short syntax or concise way of writing user-defined functions in JavaScript. This feature was introduced in ES6 or ECMAScript 2015.
For example,
let fun = () => { console.log ('This is an example of the JavaScript arrow function.'); } fun (); // Output // This is an example of the JavaScript arrow function.
21. What is meant by destructuring in JavaScript?
This is the 21st question of the top 50 JavaScript Interview Questions and Answers. Destructuring in JavaScript is used to extract or retrieve multiple values from an object & array.
- Array destructuring
let arr = [1, 3, -4, 10]; let [a, b, c, d] = arr; console.log(a, b, c ,d); // Output // 1 3 -4 10
- Object destructuring
let person = { name:"Samar Singh", age: 21, gender: 'Male' }const {name, age, gender} = person; console.log(name, age, gender); // Output // Rahul Sharma 22 Male
22. What would be the output of the JavaScript code given below?
let a = {}, b = {name:"Rohit"}, c = {name:"Singh"}; a[b] = {name:"Vivan"}; b[c] = {name:"Akshay"}; console.log(a[c]); // Output // {name: 'Vivan'}
23. How many different ways may JavaScript be included in an HTML file?
This is the 23rd question of the top 50 JavaScript Interview Questions and Answers. A JavaScript code can be included in an HTML file in three distinct ways:
- Inline
- External
- Internal
24. Mention the differences between Java & JavaScript.
Java | JavaScript |
---|---|
Java is an object-oriented programming (OOP) language. | JavaScript is an object-oriented scripting language used both client-side & server-side. |
It develops applications that run in a virtual machine or a web browser. | It develops applications that run in a web browser. |
25. What are the different methods for defining variables in JavaScript?
This is the 25th question of the top 50 JavaScript Interview Questions and Answers. There are 3 different methods for defining variables in JavaScript:
- var
- let
- const
26. Define callback function in JavaScript.
This is the 26th question of the top 50 JavaScript Interview Questions and Answers. A callback function is a function that is executed after the execution of another function. It is also defined as a function that is passed as an argument to another function.
27. Mention the use of void(0) in JavaScript.
This is the 27th question of the top 50 JavaScript Interview Questions and Answers. void(0) is used for preventing the page from refreshing or reloading.
28. How can I use JavaScript to find the Operating system on the client machine?
This is the 28th question of the top 50 JavaScript Interview Questions and Answers. We can use a navigator.appVersion property to find the Operating system on the client machine.
console.log(navigator.appVersion);
29. How to check the status of checkboxes in JavaScript?
This is the 29th question of the top 50 JavaScript Interview Questions and Answers. We can use the checked property to check the status of the checkbox in JavaScript.
30. How to detect the browser which is running the web page?
This is the 30th question of the top 50 JavaScript Interview Questions and Answers. The window object navigator is used to determine the browser in which the web page is presently executing.
console.log(navigator.appName);
31. How can a user be redirected to a different page using JavaScript?
This is the 31st question of the top 50 JavaScript Interview Questions and Answers. The window object location can be used to redirect the user to the new page.
window.location.href=https://www.usemynotes.com/;
32. Predict the output of the following JavaScript code on execution.
var x = 10; (function () { console.log("Initial Number " + x); var x = 20; console.log("New Number " + x); })(); // Output // Initial Number undefined // New Number 20
33. Name the various types of errors in JavaScript.
- Runtime errors
- Load time errors
- Logical errors
34. Mention the difference between indexOf() and lastindexOf() method in JavaScript.
indexOf()
It returns the position or index in a string of the first occurrence of the specified text.
lastIndexOf()
It returns the index of the last occurrence of specified text in a string.
let str = "Hello world"; console.log(str.indexOf("l")); console.log(str.lastIndexOf("l")); // Output // 2 // 9
35. Write a JavaScript code for adding new elements dynamically.
<!DOCTYPE html> <html> <head> <title>Elements Dynamically</title> </head> <body> <button id="btn">Click Me</button> <script> const button = document.querySelector('#btn'); button.addEventListener('click',()=>{ var p = document.createElement('h1'); var text = document.createTextNode('Add content dynamically using JavaScript'); p(text); document.body(p); }); </script> </body> </html>
36. How can you submit a form using JavaScript?
This is the 36th question of the top 50 JavaScript Interview Questions and Answers. For submitting a form using JavaScript, we can use document.form[0].submit() method.
37. In JavaScript, how do we create & read a cookie?
We can use property to create & read a cookie in JavaScript.
document.cookie = “username=Raman Singh”;
console.log(document.cookie);
38. In JavaScript, how may Generic objects be created?
var genericObject = new Object();
39. Predict the output of the following JavaScript code when executed.
console.log(false == '0') console.log(false === '0') // Output // true // false
40. Predict the result of the following JavaScript code when executed.
var a = 21; var fun = function () { console.log(a); var a = 40; }; fun (); // Output // undefined
41. Will the given JavaScript code produce an error?
var arr = [3, 2, 5]; console.log(arr[4]); No, the above code does not produce an error. // Output // undefined
42. What will be the output of the following Script when executed?
console.log(typeof undefined===typeof null); // Output // false
43. What will be the output of the following JavaScript when executed?
console.log(typeof typeof 2.4); // Output // string
44. What will be the result of the following JavaScript when executed?
var x = 1; function outerFunction(){ var x = 5; function innerFunction(){ x++; var x = 8; console.log(x) } innerFunction(); } outerFunction(); // Output // 8
45. Predict the result of the JavaScript code given below.
console.log(4 < 5 < 6); console.log(6 > 5 > 4); // Output // true // false
46. How can you create an array in JavaScript?
Using array literal
const a = [1, 3, 7];
Using new keyword
var arr = new Array(); arr[0]=3; arr[3]=1; arr[1]=5; console.log(arr); var a = new Array(3, 1.5, "Java"); console.log(a);
47. How can you create objects in JavaScript?
Using object literal
let person = { name:"Neha Singh", age: 25, gender: 'Female' }
Using new keyword
var person=new Object(); person.name='Shikhar Gupta'; person.age='24'; console.log(person);
48. What are some of the frameworks for JavaScript?
This is the 48th question of the top 50 JavaScript Interview Questions and Answers. React, Angular & Vue are some of the JavaScript frameworks.
49. In JavaScript, what method is used to remove focus from a specific object?
This is the 49th question of the top 50 JavaScript Interview Questions and Answers. In JavaScript, the Blur function is used for removing focus from an object.
50. Predict the output of both functions.
function fun1() { return { message: "hello" }; } function fun2() { return { message: "hello" }; } console.log(fun1()); console.log(fun2()); // Output // {message: 'hello'} // undefined
I hope the JavaScript Interview Questions and Answers module will be beneficial for your interviews. There are many other questions in JavaScript, but it is not possible to discuss them in a single module. You can read other programming languages tutorials & blogs on our website if you want more informative modules like this.