Photo by Unseen Studio on Unsplash
Top JavaScript Interview Questions and How to Answer Them Effectively
Frequently Asked Questions in Interviews and How to Answer Them
Table of contents
- 1. Is JavaScript a synchronous or asynchronous language?
- 2. Difference between JavaScript and Type Script
- 3. Difference between var, let, and const
- 4. What is Hoisting?
- 5. What are the data types in JavaScript?
- 6. What are the different types of operators in JavaScript?
- 7. Difference between == and ===
- 8. Difference between null and undefined
- 9. What is NaN in JavaScript?
- 10. Explain local and global scope in JavaScript
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:
var
Scope:if (true) { var x = 10; } console.log(x); // 10 (accessible outside the block)
let
Scope:if (true) { let y = 20; } console.log(y); // Error: y is not defined
const
Reassignment: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
let
andconst
: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
,bigint
Non-Primitive:
object
Examples:
Primitive Types:
let str = "Hello"; // string let num = 42; // number let isActive = true; // boolean let nothing = null; // null let notDefined; // undefined
BigInt:
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; // 8
Comparison:
console.log(10 == "10"); // true console.log(10 === "10"); // false
Logical:
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); // null
Undefined:
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); // NaN
Invalid 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(); // 10
Local Scope:
function test() { let b = 20; console.log(b); } test(); console.log(b); // Error: b is not defined