JavaScript Basics & Fundamentals
Cover core JavaScript concepts including variables, operations, data types, type coercion, equality operators, and control flow.
Downloaded from PrepIQ (https://prepiq.online)
Q1. What are the differences between var, let, and const in JavaScript?
| Keyword | Scope | Hoisting | Re-declaration | Re-assignment |
|---|---|---|---|---|
| var | Function-scoped | Hoisted, initialized as undefined | Yes | Yes |
| let | Block-scoped | Hoisted but not initialized (TDZ) | No | Yes |
| const | Block-scoped | Hoisted but not initialized (TDZ) | No | No |
function test() {
console.log(x); // undefined (hoisting)
var x = 1;
if (true) {
let y = 2; // block-scoped
const z = 3; // block-scoped, can't be reassigned
}
console.log(y); // ReferenceError: y is not defined
}Interview tip: Always use const by default, let if reassignment needed, and avoid var. Mention Temporal Dead Zone (TDZ) for let/const.
Q2. What is hoisting in JavaScript and how does it work?
Hoisting is JavaScript's behavior of moving declarations to the top of their scope before code execution.
// Variable hoisting
console.log(x); // undefined (not ReferenceError)
var x = 5;
// Function hoisting
greet(); // 'Hello' - works because function is hoisted
function greet() { console.log('Hello'); }
// let/const hoisting (Temporal Dead Zone)
console.log(y); // ReferenceError: Cannot access 'y' before initialization
let y = 10;| Type | Hoisted? | Initialized to? | Accessible before declaration? |
|---|---|---|---|
| var declaration | Yes | undefined | Yes (gives undefined) |
| let/const declaration | Yes (in TDZ) | Not initialized | No (ReferenceError) |
| Function declaration | Yes | Function body | Yes |
| Function expression | No | N/A | No |
Interview tip: Explain that hoisting happens at compile-time, not runtime. All declarations (var, let, const, function) are hoisted, but var is initialized to undefined while let/const enter a Temporal Dead Zone.
Q3. Explain JavaScript data types. What is the difference between primitive and object types?
| Type | Category | Examples | Mutable? |
|---|---|---|---|
| String, Number, Boolean | Primitive | 'hello', 42, true | No |
| undefined, null, Symbol, BigInt | Primitive | undefined, null, Symbol('id'), 123n | No |
| Object, Array, Function | Object | {}, [], function(){} | Yes |
// Primitive: passed by value
let a = 5;
let b = a;
b = 10;
console.log(a); // 5 (unchanged)
// Object: passed by reference
let obj1 = { name: 'John' };
let obj2 = obj1;
obj2.name = 'Jane';
console.log(obj1.name); // 'Jane' (changed!)Interview tip: Primitives are immutable and passed by value; objects are mutable and passed by reference. This is why typeof null === 'object' (a famous quirk).
Q4. What is scope and the scope chain in JavaScript?
Scope determines the accessibility of variables. JavaScript has global scope, function scope, and block scope (with let/const).
// Global scope
let global = 'I am global';
function outer() {
let outerVar = 'outer';
function inner() {
let innerVar = 'inner';
console.log(innerVar); // 'inner' - own scope
console.log(outerVar); // 'outer' - parent scope
console.log(global); // 'I am global' - global scope
}
inner();
}
outer();| Scope Type | Description |
|---|---|
| Global scope | Variables accessible everywhere |
| Function scope | Variables accessible only within the function |
| Block scope | Variables (let/const) accessible only within the block (if, for, etc.) |
| Scope chain | JavaScript looks up variable in local scope, then parent scopes, then global |
Interview tip: Scope chain means inner functions can access variables from outer functions, but outer functions cannot access inner function variables.
Q5. What is type coercion in JavaScript? Explain == vs ===.
Type coercion is the automatic conversion of values from one type to another. The == operator performs coercion, while === does not.
// Type coercion with ==
console.log(5 == '5'); // true (string coerced to number)
console.log(5 === '5'); // false (different types)
console.log(true == 1); // true (boolean coerced to number)
console.log(true === 1); // false
console.log(null == undefined); // true (special case)
console.log(null === undefined); // false
console.log([] == 0); // true (array coerced to number)
console.log([] === 0); // false| Operator | Name | Coercion? | Recommendation |
|---|---|---|---|
| == | Loose equality | Yes, unpredictable | Avoid |
| === | Strict equality | No, type-safe | Always use |
Interview tip: Always use === in production code. Explain some quirks like [] == 0 being true but [] == false also being true - these show why == is dangerous.
Q6. What are JavaScript falsy values and how are they evaluated in conditions?
Falsy values are values that evaluate to false in a boolean context. There are only 6 falsy values in JavaScript.
// The 6 falsy values
false
0
-0
0n (BigInt zero)
'' (empty string)
undefined
null
NaN
// Examples
if (0) console.log('This won\'t run');
if ('') console.log('This won\'t run');
if (undefined) console.log('This won\'t run');
if (null) console.log('This won\'t run');
if (NaN) console.log('This won\'t run');
if ([]) console.log('This WILL run - arrays are truthy!'); // true
if ({}) console.log('This WILL run - objects are truthy!'); // true
if ('0') console.log('This WILL run - non-empty strings are truthy!'); // trueInterview tip: Remember that empty arrays [], empty objects {}, and the string '0' are all truthy! This trips up many developers.
Q7. What is the difference between null and undefined in JavaScript?
| Aspect | null | undefined |
|---|---|---|
| Type | Object (due to a bug) | undefined |
| Meaning | Intentional absence of a value | Unintentional absence of a value |
| When assigned | By programmer explicitly | By JavaScript automatically |
| Examples | let x = null; | let x; or function(param) { } |
// undefined
let a; // declared but not assigned
console.log(a); // undefined
function test(param) {
console.log(param); // undefined if not passed
}
test();
// null
let b = null; // explicitly assigned
console.log(b); // null
// Comparison
console.log(null == undefined); // true (loose equality)
console.log(null === undefined); // false (strict equality)
console.log(typeof null); // 'object' (famous bug!)
console.log(typeof undefined); // 'undefined'Interview tip: Explain that null is intentional (developer sets it) while undefined is unintentional (JavaScript sets it). Also mention the famous bug: typeof null === 'object'.
Q8. What is the this keyword in JavaScript and how does its value get determined?
The this keyword refers to the object that the function is called on. Its value is determined at call time, not definition time.
// 1. Global context
console.log(this); // Window (browser) or global (Node.js)
// 2. Object method
const obj = {
name: 'John',
greet: function() {
console.log(this.name); // 'John' - this refers to obj
}
};
obj.greet();
// 3. Constructor function
function Person(name) {
this.name = name; // this refers to the new object being created
}
const p = new Person('Jane');
// 4. Arrow function (no own this)
const obj2 = {
name: 'Bob',
greet: () => {
console.log(this); // Window or global, not obj2!
}
};
// 5. call(), apply(), bind()
const person1 = { name: 'Alice' };
const person2 = { name: 'Charlie' };
function printName() {
console.log(this.name);
}
printName.call(person1); // 'Alice'
printName.apply(person2); // 'Charlie'
const boundFunc = printName.bind(person1);
boundFunc(); // 'Alice'| Context | this refers to |
|---|---|
| Method call (obj.method()) | The object (obj) |
| Function call (func()) | Global object or undefined (strict mode) |
| Constructor (new Class()) | The newly created instance |
| Arrow function | Lexical this from surrounding scope |
Interview tip: Emphasize that this is determined at call time, not definition time. Arrow functions don't have their own this - they use the enclosing scope's this.
Q9. What are template literals and what advantages do they offer over regular strings?
Template literals (backticks) allow string interpolation, multi-line strings, and expression evaluation - cleaner than string concatenation.
// Regular strings
const name = 'John';
const greeting = 'Hello, ' + name + '!'; // Concatenation
const multiline = 'Line 1\nLine 2'; // Need \n
// Template literals
const greeting2 = `Hello, ${name}!`; // Interpolation
const multiline2 = `Line 1
Line 2`; // Native multi-line
const expr = `5 + 3 = ${5 + 3}`; // Expression evaluation
// Tagged template literals
function highlight(strings, ...values) {
return strings.map((str, i) => str + (values[i] ? '<mark>' + values[i] + '</mark>' : '')).join('');
}
const html = highlight`Hello ${name}!`;
// 'Hello <mark>John</mark>!'| Feature | Regular strings | Template literals |
|---|---|---|
| Interpolation | String + concatenation | ${expression} |
| Multi-line | \n escape sequence | Native support |
| Readability | Hard to read long strings | Very readable |
| Tagged templates | Not possible | Supported |
Interview tip: Mention that template literals make code more readable and less error-prone. Also mention tagged templates for advanced use cases like formatting, escaping, or localization.
Q10. What is NaN and how do you check for it correctly?
NaN stands for 'Not-a-Number' but is of type 'number'. It's the only value in JavaScript that is not equal to itself!
// What produces NaN
const a = 0 / 0; // NaN
const b = parseInt('hello'); // NaN
const c = undefined + 5; // NaN
const d = Math.sqrt(-1); // NaN
// WRONG way to check for NaN
if (a == NaN) console.log('This will NEVER run!'); // Always false
if (a === NaN) console.log('This will NEVER run!'); // Always false
if (typeof a === 'number' && a != a) console.log('Hacky way'); // Works but ugly
// RIGHT way to check for NaN
if (isNaN(a)) console.log('Correct!'); // true
if (Number.isNaN(a)) console.log('Even better!'); // true - strict version
// Difference between isNaN and Number.isNaN
console.log(isNaN('hello')); // true (converts to number first)
console.log(Number.isNaN('hello')); // false (no type conversion)| Check method | Description | Type coercion? |
|---|---|---|
| NaN == NaN | WRONG - always false | N/A |
| isNaN() | Good, but coerces type | Yes |
| Number.isNaN() | BEST - strict, no coercion | No |
Interview tip: NaN !== NaN is one of the most famous JavaScript quirks. Always use Number.isNaN() for reliable checking. Mention that typeof NaN === 'number' (weird!).
JavaScript Basics & Fundamentals
Cover core JavaScript concepts including variables, operations, data types, type coercion, equality operators, and control flow.
What are the differences between var, let, and const in JavaScript?
KeywordScopeHoistingRe-declarationRe-assignmentvarFunction-scopedHoisted, initialized as undefinedYesYesletBlo...
What is hoisting in JavaScript and how does it work?
Hoisting is JavaScript's behavior of moving declarations to the top of their scope before code execution.// Va...
Explain JavaScript data types. What is the difference between primitive and object types?
TypeCategoryExamplesMutable?String, Number, BooleanPrimitive'hello', 42, trueNoundefined, null, Symbol, BigInt...
What is scope and the scope chain in JavaScript?
Scope determines the accessibility of variables. JavaScript has global scope, function scope, and block scope...
What is type coercion in JavaScript? Explain == vs ===.
Type coercion is the automatic conversion of values from one type to another. The == operator performs coercio...
What are JavaScript falsy values and how are they evaluated in conditions?
Falsy values are values that evaluate to false in a boolean context. There are only 6 falsy values in JavaScri...
What is the difference between null and undefined in JavaScript?
AspectnullundefinedTypeObject (due to a bug)undefinedMeaningIntentional absence of a valueUnintentional absenc...
What is the this keyword in JavaScript and how does its value get determined?
The this keyword refers to the object that the function is called on. Its value is determined at call time, no...
What are template literals and what advantages do they offer over regular strings?
Template literals (backticks) allow string interpolation, multi-line strings, and expression evaluation - clea...
What is NaN and how do you check for it correctly?
NaN stands for 'Not-a-Number' but is of type 'number'. It's the only value in JavaScript that is not equal to...