Most Asked JavaScript Interview Questions with Answers
Q1. What is JavaScript?
JavaScript is a programming language mainly used to make web pages interactive and dynamic. It runs in the browser, and with Node.js, it can also run on the server.
Q2. What is it used for?
JavaScript is used to:
- Change or update web page content without reloading.
- Handle user actions like clicks, forms, or animations.
- Send or receive data from a server (API calls).
- Build complete applications (both frontend and backend).
Q3. Who created JavaScript and when?
It was created by Brendan Eich in 1995 while working at Netscape. At first, it was called Mocha, then LiveScript, and later renamed JavaScript.
Q4. Is JavaScript case sensitive?
Yes, JavaScript is case sensitive. For example:
let Name = "John";
let name = "Doe";
console.log(Name); // John
console.log(name); // Doe
Here, Name
and name
are two different variables.
Q5. What is its main purpose in web development?
The main purpose of JavaScript is to make websites more interactive. It helps in handling user actions, updating content, and communicating with servers to improve the user experience.
Q6. Is JavaScript compiled or interpreted?
Traditionally, JavaScript is interpreted (executed line by line by the browser). But modern engines like Google’s V8 use JIT (Just-In-Time) compilation, which makes it much faster.
Q7. What’s the difference between compiled and interpreted languages?
-
Compiled: Code is converted to machine code before running.
-
Interpreted: Code runs line by line.
JavaScript is mainly interpreted but also uses JIT compilation to speed things up.
Q8. What are the main features of JavaScript?
- Works across all platforms and browsers.
- Supports objects and prototypes (OOP style).
- Can change HTML and CSS in real time (DOM manipulation).
- Handles events and user actions.
- Supports asynchronous programming (Promises, async/await).
Q9. How do we declare variables? Difference between var, let, const?
- var: Function-scoped, can be redeclared (old way, not much used now).
- let: Block-scoped, can be updated but not redeclared.
- const: Block-scoped, can’t be updated or redeclared (used for constants).
Q10. What are data types in JavaScript?
Primitive types: string, number, boolean, undefined, null, symbol, bigint.
Non-primitive (reference types): object, array, function.
Q11. What is hoisting?
Hoisting means JavaScript moves variable and function declarations to the top of their scope before running the code.
var
is hoisted but initialized asundefined
.let
andconst
are hoisted too, but you can’t use them before declaring.- Function declarations are fully hoisted.
Q12. What is DOM and how does JS use it?
The DOM (Document Object Model) represents a web page as a tree of objects. JavaScript uses it to add, remove, or update HTML elements and styles.
Q13. How do you write a function?
function greet() {
console.log("Hello, World!");
}
greet();
Q14. What are conditional statements?
They let us run code only if a condition is true. Example:
if (age < 18) { ... }
else if (age === 18) { ... }
else { ... }
Q15. Why do we use loops?
Loops let us run the same code multiple times.
- for loop: runs for a set number of times.
- while loop: runs while a condition is true.
Q16. What is an array?
An array is a collection of values stored in one variable. Example:
let fruits = ["Apple", "Banana", "Mango"];
Q17. What is an object?
An object stores data in key–value pairs. Example:
let person = { name: "John", age: 25 };
Q18. Difference between == and === ?
==
compares values only.===
compares values and types.
Q19. What are operators in JS?
- Arithmetic:
+ - * / %
- Comparison:
> < == ===
- Logical:
&& || !
Q20. How do you write comments?
- Single-line:
// comment
- Multi-line:
/* comment */
Q21. Difference between null and undefined?
- undefined: variable declared but not assigned.
- null: an intentional empty value.
Q22. How to include JS in HTML?
Using the <script>
tag.
Q23. What is console.log()?
It’s used to print messages in the console, mostly for debugging.
Q24. What is scope?
Scope defines where a variable can be accessed.
- Global scope
- Function scope
- Block scope
Q25. What are built-in methods?
Predefined functions like:
- String →
"hello".toUpperCase()
- Array →
[1,2,3].push(4)
Q26. How do you take user input?
Using prompt()
or HTML forms.
Q27. What are events in JS?
Events are actions (like click, input, hover). We can handle them using event listeners.
Q28. What does return do in a function?
It sends a value back to the code that called the function.
Q29. Other data structures apart from array & object?
- Map, Set, WeakMap, WeakSet.
Q30. What is NaN?
NaN means Not-a-Number. It shows up when a math operation is invalid.
Q31. What are template literals?
Strings written with backticks () that support variables inside
${}`.
let name = "John";
console.log(`Hello, ${name}!`);
Q32. Difference between function declaration and expression?
- Declaration: Hoisted, can be used before defined.
- Expression: Assigned to a variable, not hoisted.
Q33. What is an arrow function?
A shorter way to write functions. Example:
const add = (a, b) => a + b;
Q34. Difference between break and continue?
- break: exits the loop.
- continue: skips the current iteration.
Q35. What does typeof do?
It shows the type of a value. Example:
typeof 123 // "number"
Q36. Difference between innerHTML and textContent?
- innerHTML: gets/sets HTML along with tags.
- textContent: gets/sets only plain text.