JavaScript Interview Questions 1-5 (Data Types, Equality, Declarations, Hoisting, Scope)
This lesson covers five fundamental JavaScript topics common in interviews: data types, the difference between double and strict equality, how variable declarations differ (var, let, const), what hoisting means, and how scope works in JavaScript. Each question includes a clear explanation, real-world analogies, and runnable code examples with sample outputs so you feel confident explaining and demonstrating these concepts in an interview.
The tone is calm and supportive. Think of these concepts as tools in a toolbox: once you know when and how to use them, you build reliable, maintainable code. Ready? Let’s go.
1. What are the different data types in JavaScript?
JavaScript has a set of core primitive types and one composite type. Understanding these is like knowing the kinds of containers you can use: each holds values with particular behaviors.
- Undefined — a variable declared but not assigned a value.
- Null — an explicit "empty" value; intentionally set to mean "no value".
- Boolean — true or false.
- Number — all numeric values (integers and floating point).
- BigInt — integers beyond the safe Number range for precise integer math.
- String — text values.
- Symbol — unique identifiers (rare in everyday beginner code but important for library authors).
- Object — composite type for collections: arrays, functions, plain objects, dates, etc.
Analogy: Think of primitives as single boxed items (a single book, a single coin) and objects as a toolbox that can hold many things with named slots.
// Sample: creating values of each type
let a; // undefined
let b = null; // null
let c = true; // boolean
let d = 42; // number
let e = 9007199254740991n; // BigInt
let f = 'hello'; // string
let g = Symbol('id'); // symbol
let h = { x: 1 }; // object (plain)
let arr = [1, 2, 3]; // object (array)
function fn() {} // object (function)
console.log(typeof a); // "undefined"
console.log(typeof b); // "object" <-- historic quirk: null reports object
console.log(typeof c); // "boolean"
console.log(typeof e); // "bigint"
console.log(typeof arr); // "object"
Sample output:
undefined
object
boolean
bigint
object
Tip: Remember the null "typeof" quirk. Use strict checks (value === null) when you need to detect null explicitly.
2. What is the difference between == and ===?
== is loose equality and attempts type coercion before comparing. === is strict equality and compares both type and value without coercion. In interviews, prefer === unless you specifically want coercion and can explain why.
Analogy: == is like comparing two ID cards by trying to convert formats first (maybe compare names after normalizing), while === requires both the format and content to match exactly.
// Examples
console.log(0 == '0'); // true (string '0' coerced to number 0)
console.log(0 === '0'); // false (different types: number vs string)
console.log(false == ''); // true ('' coerced to false)
console.log(false === ''); // false
console.log(null == undefined); // true (special-case loose equality)
console.log(null === undefined); // false
// Be explicit if coercion helps, otherwise use ===
Sample output:
true
false
true
false
true
false
3. What is the difference between var, let, and const?
These keywords declare variables but differ in scope, redeclaration, and mutability rules.
- var: function-scoped or globally scoped. Can be redeclared and updated. It also participates in hoisting in a way that sets the variable to undefined before code runs.
- let: block-scoped (best for most variables). Can be updated but not redeclared in the same scope. Not initialized before declaration (temporal dead zone applies).
- const: block-scoped and must be initialized at declaration. Cannot be reassigned, but if the value is an object or array, its contents can be mutated.
// var example (hoisted, function-scoped)
function varExample() {
if (true) {
var x = 1;
}
console.log(x); // 1, because var is function-scoped
}
// let example (block-scoped)
function letExample() {
if (true) {
let y = 2;
}
// console.log(y); // ReferenceError: y is not defined
}
// const example (block-scoped, immutable binding)
const obj = { name: 'Alice' };
obj.name = 'Bob'; // allowed: mutating object
// obj = {}; // TypeError: Assignment to constant variable.
varExample();
letExample();
console.log(obj.name); // Bob
Sample output:
1
Bob
4. What is hoisting in JavaScript?
Hoisting is a behavior where variable and function declarations are conceptually moved to the top of their containing scope before code execution. For var, a variable is hoisted and initialized as undefined. For let and const, declarations are hoisted but not initialized (they remain in the temporal dead zone until evaluated). Function declarations are hoisted with their body.
Analogy: Hoisting is like the stage crew placing props on stage before the actors run the scene. Some props are present but empty (var -> undefined). Some props are locked in a box until the cue (let/const -> temporal dead zone).
// var is hoisted and initialized to undefined
console.log(a); // undefined
var a = 10;
// function declarations are hoisted fully
console.log(sum(2, 3)); // 5
function sum(x, y) {
return x + y;
}
// let/const are hoisted but not initialized (TDZ)
console.log(b); // ReferenceError
let b = 20;
Note: Do not rely on hoisting to structure code. Declare variables near their first use for readability and fewer surprises.
Sample behavior (expected):
undefined
5
// then a ReferenceError for accessing b before declaration
5. What is scope in JavaScript?
Scope defines the visibility and lifetime of variables. JavaScript has global scope, function scope, and block scope (with let/const). Modern JS encourages block scoping to avoid accidental variable leaks.
- Global scope: variables available everywhere (avoid polluting global scope).
- Function scope: var is scoped to the entire function.
- Block scope: let and const are limited to the nearest enclosing braces (if, for, while, etc.).
Example showing differences:
// Global scope
var g = 'globalVar';
function outer() {
var f = 'functionVar'; // function-scoped
if (true) {
let b = 'blockVar'; // block-scoped
var v = 'varInIf'; // function-scoped despite being inside block
console.log(b); // 'blockVar'
}
// console.log(b); // ReferenceError
console.log(v); // 'varInIf'
console.log(f); // 'functionVar'
}
outer();
console.log(g); // 'globalVar'
Sample output:
blockVar
varInIf
functionVar
globalVar
Final tip: Use let and const by default. Prefer const for values that should not be re-assigned, and let when reassignment is necessary. Avoid var unless you need function-scoped behavior for legacy reasons.
That completes these five foundational questions. Practice by writing small programs that intentionally exercise hoisting, scope, and equality rules so you can both explain the concept and show working examples during an interview. You’re doing great—clear explanations plus short runnable examples will make a strong impression.