๐ JavaScript Interview Questions with Answers โ Part 1
๐ง 1. What is JavaScript and what is it used for?
JavaScript is a high-level, interpreted programming language mainly used to make websites interactive and dynamic. It runs inside the browser and can also run on servers using Node.js.
Common Uses of JavaScript:
โข Building interactive websites
โข Form validation
โข Animations and sliders
โข API calls and dynamic content
โข Web apps and mobile apps
โข Backend development with Node.js
โข Game development
Example: console.log("Hello World");
2. What are the data types in JavaScript?
JavaScript has two categories of data types:
Primitive Data Types
1. String
2. Number
3. Boolean
4. Undefined
5. Null
6. BigInt
7. Symbol
Reference Data Types
1. Object
2. Array
3. Function
Example:
let name = "Deepak"; // String
let age = 25; // Number
let isActive = true; // Boolean
let data = null; // Null
let value; // Undefined
3. What is the difference between null and undefined?
Feature | null | undefined
Meaning | Intentional empty value | Variable not assigned
Type | object | undefined
Assigned by | Developer | JavaScript automatically
Example:
let a = null;
let b;
console.log(a); // null
console.log(b); // undefined
Key Point:
โข null means โempty intentionallyโ
โข undefined means โvalue not assigned yetโ
4. What is the difference between == and ===?
== (Loose Equality)
โข Compares values only
โข Performs type conversion (type coercion)
=== (Strict Equality)
โข Compares both value and data type
โข No type conversion
Example:
console.log(5 == "5"); // true
console.log(5 === "5"); // false
Interview Tip: Always prefer === because it gives more predictable results.
5. What are primitive vs reference types?
Primitive Types
Stored directly in memory.
Examples: String, Number, Boolean, Null, Undefined
Reference Types
Stored by reference (memory address).
Examples: Objects, Arrays, Functions
Example:
let a = 10;
let b = a;
b = 20;
console.log(a); // 10
Reference Example:
let obj1 = {name: "John"};
let obj2 = obj1;
obj2.name = "Mike";
console.log(obj1.name); // Mike
Key Difference:
โข Primitive โ copied by value
โข Reference โ copied by reference
6. What is type coercion?
Type coercion means JavaScript automatically converts one data type into another during operations or comparisons.
Example:
console.log("5" + 2); // "52"
console.log("5" - 2); // 3
Why?
โข + prefers string concatenation
โข - converts strings to numbers
Types of Coercion:
1. Implicit coercion (automatic)
2. Explicit coercion (manual)
Explicit Example: Number("10"); // 10 , String(123); // "123"
7. What is the difference between let, const, and var?
Feature | var | let | const
Scope | Function | Block | Block
Reassign | Yes | Yes | No
Redeclare | Yes | No | No
Hoisted | Yes | Yes | Yes
Example:
var a = 10;
let b = 20;
const c = 30;
Key Points:
โข Use let for changing values
โข Use const for fixed values
โข Avoid var in modern JavaScript
8. What is block-scope vs function-scope?
Function Scope
Accessible inside the entire function. var is function-scoped.
Block Scope
Accessible only inside {} block. let and const are block-scoped.
Example:
function test() {
if (true) {
var a = 10;
let b = 20;
}
console.log(a); // Works
console.log(b); // Error
}
9. What is the difference between let and var?
Feature | var | let
Scope | Function | Block
Redeclare | Allowed | Not allowed
Hoisting | Yes | Yes (TDZ applies)
Example:
var x = 10;
var x = 20; // Allowed
let y = 10;
let y = 20; // Error
Important: let avoids many bugs caused by var.
10. How do you declare and use variables?
Variables are used to store data.
Syntax:
let name = "Deepak";
const age = 25;
Rules:
- Use meaningful names
- Cannot start with numbers
- Case-sensitive
Double Tap โค๏ธ For Part-2
Post #3399
2.49K
- โค 13
- ๐ 1
- ๐คฃ 1
- ๐ญ 1
- ๐ฟ 1