1. Operators
JavaScript provides two primary equality comparison operators:
==(loose equality): performs type coercion before comparing values.===(strict equality): compares both value and type without coercion.
Example:
let age1 = 20;
let age2 = "20";
console.log(age1 == age2); // true (after string-to-number coercion)
console.log(age1 === age2); // false (string vs number)
Common type conversions:
- String to Number: Use
parseInt()or unary+. Non-numeric strings yieldNaN. - Boolean to Number:
true → 1,false → 0. - Any to Boolean:
0andNaN→false- Empty string
""→false null,undefined→false- All other values →
true
Example usage for truthy/falsy checks:
let val = +"42";
console.log(parseInt(val) + 1); // 43
let flag = null;
if (!flag) {
console.log("null is falsy"); // This runs
}
let input = "hello";
if (input) {
console.log("Non-empty string is truthy"); // This runs
}
2. Control Flow Statements
JavaScript supports standard control structures similar to Java:
if,else if,elseswitchfor,while,do...whileloopsbreakandcontinue
Example:
for (let i = 0; i < 3; i++) {
if (i === 1) continue;
console.log(i); // Outputs: 0, 2
}
3. Functions
Functions can be declared or assigned as expressions (anonymous functions).
- Named function declaration
- Function expression (asssignable to variables)
Key characteristics:
- Parameter count is not enforced — extra arguments are ignored; missing ones become
undefined. - All arguments accessible via the
argumentsobject (or modern spread syntax).
Example:
const sum = function(a, b) {
return a + b;
};
console.log(sum(1, 2)); // 3
console.log(sum(1, 2, 9)); // 3 (third argument ignored)
console.log(sum(5)); // NaN (b is undefined)
4. Arrays
JavaScript arrays are dynamic, type-flexible, and zero-indexed.
Creation Methods
// Array constructor
let arr1 = new Array(10, 20, 30);
// Literal syntax (preferred)
let arr2 = [10, 20, 30];
Behavior and Features
- Dynamic length: Accessing beyond existing indices fills gaps with
undefined. - Heterogeneous types: Elements may hold different data types.
lengthproperty reflects highest index + 1 (even if sparse).
Example:
let mixed = [1, "two", true, {x: 5}];
mixed[5] = "gap";
console.log(mixed[4]); // undefined
console.log(mixed.length); // 6
Common Methods
push(...items)— append elements to endsplice(start, deleteCount, ...items)— add/remove items at specific indices
Example usage:
let nums = [1, 2, 3];
nums.push(4);
console.log(nums); // [1, 2, 3, 4]
nums.splice(1, 2); // remove 2 items starting at index 1
console.log(nums); // [1, 4]