Top JavaScript Interview Questions and How to Answer Them Effectively
Frequently Asked Questions in Interviews and How to Answer Them

Hello there! My name is Indrajeet, and I am a skilled web developer passionate about creating innovative, user-friendly, and dynamic web applications. 📌My Expertise advanced proficiency in angular front-End Development. Back-End Development Leveraging the power of .NET, I build secure, robust, and scalable server-side architectures.
1. Is JavaScript a synchronous or asynchronous language?
Answer: JavaScript is single-threaded, meaning it executes code line by line in a synchronous manner by default. However, it has asynchronous capabilities through mechanisms like callbacks, Promises, and async/await. This makes it non-blocking, allowing certain tasks (like network requests) to run in the background without halting the main thread.
Examples:
Synchronous Execution:
console.log("Step 1"); console.log("Step 2"); console.log("Step 3"); // Output: Step 1, Step 2, Step 3 (in order)Asynchronous Execution:
console.log("Start"); setTimeout(() => console.log("Asynchronous Task"), 1000); console.log("End"); // Output: Start, End, Asynchronous Task (asynchronous is delayed)Using Promises:
console.log("Start"); fetch("https://jsonplaceholder.typicode.com/posts/1") .then(response => response.json()) .then(data => console.log(data)); console.log("End"); // Output: Start, End, then the fetched data (asynchronously)using set timeout method :
console.log("Start");
setTimeout(() => {
console.log("Async Operation");
}, 1000);
console.log("End");
Explanation:
Output:
Start
End
Async Operation
The setTimeout function executes after the main thread completes, demonstrating asynchronous behavior.
2. Difference between JavaScript and Type Script
Answer:
| Feature | JavaScript | Type Script |
| Definition | A scripting language for web pages. | A super-set of JavaScript with static typing. |
| Typing | Dynamically typed. | Statically typed (with types like number, string). |
| Compilation | Interpreted in the browser. | Compiles to JavaScript. |
| Error Checking | Runtime errors. | Compile-time errors. |
| Features | No extra features | Includes types, interfaces, etc. |
| Learning Curve | Easier | Requires understanding of types |
Examples:
JavaScript Code:
let x = "Hello"; x = 42; // No error (dynamic typing)TypeScript Code:
let x : string = "Hello"; x = 42; // Error: Type 'number' is not assignable to type 'string'.Static Typing in TypeScript:
function add(a: number, b: number): number { return a + b; } console.log(add(5, 10)); // Valid console.log(add(5, "10")); // Error
3. Difference between var, let, and const
Explanation:
| Feature | var | let | const |
| Scope | Function-scoped. | Block-scoped. | Block-scoped. |
| Re-declaration | Allowed. | Not allowed. | Not allowed. |
| Re-assignment | Allowed. | Allowed. | Not allowed. |
| Hoisting | Hoisted with undefined. | Hoisted without initialization. | Hoisted without initialization. |
Examples:
varScope:if (true) { var x = 10; } console.log(x); // 10 (accessible outside the block)letScope:if (true) { let y = 20; } console.log(y); // Error: y is not definedconstReassignment:const z = 30; z = 40; // Error: Assignment to constant variable
4. What is Hoisting?
Answer: Hoisting is JavaScript's behavior of moving variable and function declarations to the top of their scope before execution. var declarations are hoisted with an undefined value, while let and const are hoisted but remain uninitialized.
Examples:
Hoisting with
var:console.log(a); // undefined var a = 10;Hoisting with
letandconst:console.log(b); // Error: Cannot access 'b' before initialization let b = 20;Function Hoisting:
greet(); // Hello function greet() { console.log("Hello"); }Example:
console.log(a); // Output: undefined
var a = 10;
console.log(b); // ReferenceError: Cannot access 'b' before initialization
let b = 20;
Explanation:var is hoisted with undefined, but let and const are in the temporal dead zone.
5. What are the data types in JavaScript?
Explanation: JavaScript has 7 primitive data types and 1 non-primitive type:
Primitive:
string,number,boolean,null,undefined,symbol,bigintNon-Primitive:
object
Examples:
Primitive Types:
let str = "Hello"; // string let num = 42; // number let isActive = true; // boolean let nothing = null; // null let notDefined; // undefinedBigInt:
let bigNumber = 123456789012345678901234567890n;Objects:
let obj = { name: "John", age: 30 };
6. What are the different types of operators in JavaScript?
Explanation:
Arithmetic:
+,-,*,/,%,**Comparison:
==,===,!=,!==,<,>,<=,>=Logical:
&&,||,!Assignment:
=,+=,-=Bitwise:
&,|,~,^,<<,>>
Examples:
Arithmetic:
let sum = 10 + 5; // 15 let power = 2 ** 3; // 8Comparison:
console.log(10 == "10"); // true console.log(10 === "10"); // falseLogical:
console.log(true && false); // false
7. Difference between == and ===
Explanation:
==: Loose equality, compares values after type conversion (compares values only)===: Strict equality, compares values without type conversion (compares values along with data type)
Examples:
==:console.log(10 == "10"); // true===:console.log(10 === "10"); // false
8. Difference between null and undefined
Explanation:
null: Explicit absence of value.undefined: Uninitialized variable.
Examples:
Null:
let a = null; console.log(a); // nullUndefined:
let b; console.log(b); // undefined
9. What is NaN in JavaScript?
Explanation:
NaN(Not-a-Number) is a value that represents an invalid number.
Examples:
Arithmetic Error:
console.log(0 / 0); // NaNInvalid Parsing:
console.log(Number("abc")); // NaN
10. Explain local and global scope in JavaScript
Explanation:
Local Scope: Variables declared inside functions.
Global Scope: Variables declared outside functions, accessible globally.
Examples:
Global Scope:
let a = 10; function test() { console.log(a); } test(); // 10Local Scope:
function test() { let b = 20; console.log(b); } test(); console.log(b); // Error: b is not defined





