Skip to content

JavaScript Basics (Very Important) ​

These topics are asked in almost every JavaScript and React interview.


1. Variables (var, let, const) ​

Variables are used to store data.

var ​

  • Old way to declare variables
  • Function scoped
  • Can be redeclared
  • Can be reassigned
  • Hoisted with undefined
js
var name = "Dipak";
var name = "Rahul"; // allowed

console.log(name); // Rahul

Problem with var ​

js
if (true) {
  var age = 25;
}

console.log(age); // 25

var ignores block scope, which can create bugs.


let ​

  • Introduced in ES6
  • Block scoped
  • Cannot be redeclared
  • Can be reassigned
js
let city = "Kolkata";
city = "Delhi"; // allowed

console.log(city);

Block Scope ​

js
if (true) {
  let score = 90;
}

// console.log(score); // Error

const ​

  • Block scoped
  • Cannot be redeclared
  • Cannot be reassigned
  • Value must be initialized
js
const pi = 3.14;

Important ​

For objects and arrays, contents can still change.

js
const user = {
  name: "Dipak",
};

user.name = "Rahul"; // allowed

But:

js
// user = {} // Error

Difference Between var, let, and const ​

Featurevarletconst
ScopeFunctionBlockBlock
ReassignYesYesNo
RedeclareYesNoNo
HoistingYesYesYes
Temporal Dead ZoneNoYesYes

2. Data Types ​

JavaScript has two main categories of data types.


Primitive Data Types ​

These store single values.

TypeExample
String"Hello"
Number10
Booleantrue
Undefinedundefined
Nullnull
BigInt123n
SymbolSymbol()

Example ​

js
let name = "Dipak";
let age = 24;
let isActive = true;
let data = null;

Non-Primitive Data Types ​

These store collections of data.

  • Object
  • Array
  • Function

Example ​

js
const user = {
  name: "Dipak",
  age: 24,
};

const skills = ["JS", "React"];

3. Type Conversion ​

Changing one data type into another.


String to Number ​

js
let num = Number("10");

console.log(num); // 10

Number to String ​

js
let str = String(100);

console.log(str); // "100"

Boolean Conversion ​

js
Boolean(1); // true
Boolean(0); // false

Implicit Conversion (Coercion) ​

JavaScript automatically converts types.

js
"5" + 1; // "51"
"5" - 1; // 4

4. Operators ​

Operators perform operations on values.


Arithmetic Operators ​

js
+  // addition
-  // subtraction
*  // multiplication
/  // division
%  // modulus

Example:

js
console.log(10 + 5);
console.log(10 % 3);

Comparison Operators ​

js
==
===
!=
>
<
>=
<=

Example:

js
console.log(5 == "5"); // true
console.log(5 === "5"); // false

Logical Operators ​

js
&& // AND
|| // OR
!  // NOT

Example:

js
true && false; // false

5. Conditions ​

Used for decision making.


if Statement ​

js
let age = 20;

if (age >= 18) {
  console.log("Adult");
}

if...else ​

js
let marks = 40;

if (marks >= 50) {
  console.log("Pass");
} else {
  console.log("Fail");
}

else if ​

js
let score = 85;

if (score >= 90) {
  console.log("A");
} else if (score >= 70) {
  console.log("B");
} else {
  console.log("C");
}

Ternary Operator ​

Short form of if...else.

js
let age = 18;

let result = age >= 18 ? "Adult" : "Minor";

6. Loops ​

Loops repeat code multiple times.


for Loop ​

js
for (let i = 0; i < 5; i++) {
  console.log(i);
}

while Loop ​

js
let i = 0;

while (i < 5) {
  console.log(i);
  i++;
}

do...while ​

Runs at least once.

js
let i = 0;

do {
  console.log(i);
  i++;
} while (i < 5);

for...of ​

Used for arrays.

js
const arr = [1, 2, 3];

for (const item of arr) {
  console.log(item);
}

for...in ​

Used for objects.

js
const user = {
  name: "Dipak",
  age: 24,
};

for (const key in user) {
  console.log(key);
}

7. Functions ​

Functions are reusable blocks of code.


Normal Function ​

js
function greet() {
  console.log("Hello");
}

greet();

Function with Parameters ​

js
function add(a, b) {
  return a + b;
}

console.log(add(2, 3));

Arrow Function ​

Short syntax introduced in ES6.

js
const add = (a, b) => a + b;

Anonymous Function ​

Function without a name.

js
const greet = function () {
  console.log("Hi");
};

Callback Function ​

Function passed into another function.

js
function greet(name, callback) {
  console.log(name);
  callback();
}

greet("Dipak", function () {
  console.log("Done");
});

8. Arrays ​

Arrays store multiple values.

js
const fruits = ["Apple", "Banana", "Mango"];

Common Array Methods ​

push() ​

Adds item at end.

js
fruits.push("Orange");

pop() ​

Removes last item.

js
fruits.pop();

shift() ​

Removes first item.

js
fruits.shift();

unshift() ​

Adds item at beginning.

js
fruits.unshift("Grapes");

map() ​

Returns new array.

js
const nums = [1, 2, 3];

const doubled = nums.map((n) => n * 2);

filter() ​

Filters data.

js
const nums = [1, 2, 3, 4];

const even = nums.filter((n) => n % 2 === 0);

9. Objects ​

Objects store data in key-value pairs.

js
const user = {
  name: "Dipak",
  age: 24,
};

Access Object Values ​

js
console.log(user.name);
console.log(user["age"]);

Add New Property ​

js
user.city = "Kolkata";

Delete Property ​

js
delete user.age;

10. Scope ​

Scope defines where variables are accessible.


Global Scope ​

js
let name = "Dipak";

function test() {
  console.log(name);
}

Function Scope ​

js
function demo() {
  let age = 20;
}

// console.log(age); // Error

Block Scope ​

js
if (true) {
  let city = "Kolkata";
}

// console.log(city); // Error

11. Hoisting ​

JavaScript moves declarations to the top before execution.


var Hoisting ​

js
console.log(a);

var a = 10;

Internally:

js
var a;

console.log(a); // undefined

a = 10;

let and const ​

js
console.log(b);

let b = 20;

This gives:

js
ReferenceError;

Because of the Temporal Dead Zone (TDZ).


12. Template Literals ​

Used for dynamic strings.

Uses backticks ` `.

js
const name = "Dipak";

console.log(`Hello ${name}`);

Multi-line Strings ​

js
const msg = `
Hello
Welcome
`;

13. Difference Between == and === ​

== (Loose Equality) ​

Checks value only.

js
"5" == 5; // true

JavaScript converts types automatically.


=== (Strict Equality) ​

Checks value and type.

js
"5" === 5; // false

Interview Tips ​

Frequently Asked Topics ​

  • Hoisting
  • Scope
  • Closures
  • Array methods
  • Objects
  • Event loop
  • Promises
  • this keyword
  • DOM
  • Async/Await

Best Practice ​

  • Prefer const by default
  • Use let when value changes
  • Avoid var
  • Always use ===
  • Write small reusable functions

© 2025 DDocs · Dipak's Documentation Guide