JavaScript Interview Notes with Real-Time Examples
1. Data Types
JavaScript has two types of data types:
Primitive: String, Number, Boolean, Null, Undefined, Symbol, BigInt
Non-Primitive: Object, Array, Function
Example:
let name = "Alice"; // string
let age = 25; // number
let isStudent = false; // boolean
let user = { name, age }; // object
2. Functions
Functions are blocks of code designed to perform tasks.
Example:
function greet(name) {
return "Hello " + name;
Arrow function:
const add = (a, b) => a + b;
3. Hoisting
Hoisting moves declarations to the top. Only declarations are hoisted, not initializations.
Example:
[Link](a); // undefined
var a = 5;
JavaScript Interview Notes with Real-Time Examples
4. Closures
A closure is a function having access to the parent scope, even after the parent function has closed.
Example:
function outer() {
let count = 0;
return function() {
return ++count;
const counter = outer();
counter(); // 1
5. Scope
Scope determines variable access. Types:
- Global Scope
- Function Scope
- Block Scope (let, const)
Example:
if (true) {
let a = 10; // accessible only inside block
6. Asynchronous JS
JavaScript handles async with Callbacks, Promises, and async/await.
Example with Promise:
fetch(url)
JavaScript Interview Notes with Real-Time Examples
.then(res => [Link]())
.then(data => [Link](data));
Example with async/await:
async function loadData() {
const res = await fetch(url);
const data = await [Link]();
[Link](data);
7. Array Methods
Common methods: map, filter, reduce, find, some
Example:
const nums = [1, 2, 3];
const doubled = [Link](n => n * 2); // [2, 4, 6]
8. Object Destructuring
Extract values from objects easily.
Example:
const user = { name: 'Eva', age: 30 };
const { name, age } = user;
9. Spread and Rest Operators
Spread (...) expands values, Rest (...) gathers values.
Example:
const arr = [1, 2];
JavaScript Interview Notes with Real-Time Examples
const newArr = [...arr, 3]; // [1, 2, 3]
function sum(...nums) {
return [Link]((a, b) => a + b);
10. Prototypes & Inheritance
Prototypes allow sharing methods.
Example:
function Person(name) {
[Link] = name;
[Link] = function() {
return `Hi ${[Link]}`;
};
11. Event Loop
Handles async execution in JS.
Example:
[Link]("1");
setTimeout(() => [Link]("2"), 0);
[Link]("3"); // Output: 1, 3, 2
12. 'this' Keyword
'this' refers to the object that owns the method.
Arrow functions don't bind their own 'this'.
JavaScript Interview Notes with Real-Time Examples
Example:
const obj = {
value: 10,
show: function() { [Link]([Link]); }
};
13. Type Coercion
JS automatically converts types.
Example:
'5' + 1 = '51'
'5' - 1 = 4
14. ES6+ Features
- let, const
- Arrow functions
- Template literals: `Hello ${name}`
- Destructuring
- Spread/Rest
- Modules (import/export)
- Optional chaining
Example:
const person = { name: 'John', address: { city: 'NY' } };
[Link](person?.address?.city);
15. Real-Time Example
Email validation in a form:
JavaScript Interview Notes with Real-Time Examples
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if () {
setError("Invalid email");