0. Language overview
Data typesVariablesOperatorsGrammarControl structuresObjectsArraysFunctionsClassesAsynchronous programmingModulesLanguage and runtimeFurther exploration
1.Grammar and Types
1.Basics
Code Structure
// Single statement : Instruction that perform actions.
console.log("Hello, world!");
// Expressions - Produce values
2 + 2;
// Block of code: {} - Group multiple statements together.
{
let a = 5;
let b = 10;
console.log(a + b); // Output: 15
}
Case Sensitivity & Unicode
// Case Sensitivity
let name = "Alice";
let Name = "Bob";
console.log(name); // Output: Alice
console.log(Name); // Output: Bob
// Unicode
let π = 3.14159;
let こんにちは = "Hello in Japanese";
console.log(π); // Output: 3.14159
console.log(こんにちは); // Output: Hello in Japanese
Whitespace
// These are equivalent
let x = 5;
let y=10;
console.log(x + y); // Output: 15
// javascript ignores extra spaces, tabs, and newlines between statements.
Semicolons (Optional vs Required)
// without semicolons
let a = 10
// with semicolons (recommended for clarity)
let b = 20;
console.log(a + b) // Output: 30
// (;) are optional in JavaScript because of Automatic Semicolon Insertion (ASI)
// Pitfall -- after return JavaScript inserts ; automatically.
// so we should put { on the same line as return
strict mode- Prevents accidental global variables.
- Disallows using reserved keywords as variable names.
- Prevents Deleting Variables & Functions
- Prevents Duplicates in Function Parameters
- Prevents this from Being window (Safer Function Execution)
- Prevents Writing to Read-Only Properties
- Prevents Modifying arguments Object
- Overall throws errors for bad syntax.
"use strict";
x = 10; // ReferenceError: x is not defined
var public = "Restricted word!";
// SyntaxError: Unexpected strict mode reserved word
var myVar = 10;
delete myVar; // SyntaxError: Delete of an unqualified identifier in strict mode
function sum(a, a) { // SyntaxError: Duplicate parameter name not allowed
return a + a;
}
function showThis() {
console.log(this); // undefined
}
showThis(); // Output: undefined
const obj = Object.freeze({ name: "Alice" });
// to use freeze we have to use strict mode else it won't work.
obj.name = "Bob"; // TypeError: Cannot assign to read only property 'name'
function updateValue(x) {
arguments[0] = 20;
console.log(x); // Output: 10 (unchanged)
// without strict mode Output: 20
}
updateValue(10);
2.Comments
- Single-line Comments (
//) - Multi-line Comments (
/*...*/) - Documentation Comments (
/** ... **/) - Hashbang comments (eg:-
#!/usr/bin/env node)
3.Declarations
- Variables (
var,let,const)var- var can be declaired without initialized together.
- var can be redeclaired with a new value.
- var can be reasigned to new value.
- var is hoisted globally.
let- let can be declaired without initialized together.
- let can NOT be redecliared.
- let can be reasigned to new value.
- let is NOT hoisted globally.
const- const must initialized while declairation.
- const can NOT be redecliared.
- const can NOT be reasigned.
- const is NOT hoisted globally.
- identifiers (Letters (
A-Z, a-z), Digits (0-9), Underscore (_)) - Declaring variables (
var x) - Declaration and initialization (
var x = "Hello",let y = 2) - Variable Scope
{} - Variable hoisting (
ReferenceError,temporal dead zone&function hoisting) - Global variables (
global object,window,globalThis) - Constants (
const z = "Hello World!")
4.Data Structures and Types
Primitive Types
StringNumberBooleanSymbolNullBigIntUndefined
object / Reference Types
Object(built-in constructor function)Array(Subtype of Object)Function(Subtype of Object)Date(Built-in Object for handling dates)RegExp(Regular Expression Object)Map(Key-value pair with unique keys)Set(Collection of unique values)WeakMap(Similar to Map, but holds weak references)WeakSet(Similar to Set, but holds weak references)promise(Represents asynchronous operations)Error(Built-in error handling object)
Type Conversion and Coercion
- Type coercion is JavaScript's automatic conversion of values between different types (string, number, boolean, etc.) during operations.
- JavaScript is a dynamically typed language, meaning a variable's type can change at runtime.
Explicit Coercion
- Type Conversion: You manually convert values using String(), Number(), Boolean().
- To avoid unexpected behavior, manually convert types using:
Number()→ Converts to a numberString()→ Converts to a stringBoolean()→ Converts to true or false
2.1 Converting to Numbersconsole.log(Number("10")); // 10 console.log(Number("10.5")); // 10.5 console.log(Number("")); // 0 console.log(Number("Hello")); // NaN (Not a Number) console.log(Number(true)); // 1 console.log(Number(false)); // 0 console.log(parseInt("42px")); // 42 (Ignores non-numeric part) console.log(parseFloat("3.14meters")); // 3.142.2 Converting to Stringsconsole.log(String(42)); // "42" console.log(String(true)); // "true" console.log(String(false)); // "false" console.log(String(null)); // "null" console.log(String(undefined)); // "undefined"2.3 Converting to Booleansconsole.log(Boolean(1)); // true console.log(Boolean("Hello")); // true console.log(Boolean(0)); // false console.log(Boolean("")); // false console.log(Boolean(null)); // false console.log(Boolean(undefined)); // false console.log(Boolean([])); // ture console.log(Boolean({})); // ture
Implicit Coercion
- Type Coercion: JavaScript automatically converts types when needed.
1.1 String Coercion(Number → String)- If one operand is a string, JavaScript converts the other operand to a string.
console.log("5" + 3); // "53" (Number 3 converted to String) console.log(10 + "20"); // "1020" console.log("Hello " + 5); // "Hello 5"1.2 Numeric Coercion(String → Number)- JavaScript treats -, , /, %, * as numeric operations, so it converts strings to numbers.
- Exception: + always prefers string concatenation
console.log("10" - 2); // 8 (String "10" converted to Number) console.log("4" * "2"); // 8 (Both strings converted to numbers) console.log("6" / "3"); // 2 console.log("9" - "1" + 2); // 10 console.log("10" - "5" - "2"); // 3 console.log("2" ** 3); // 81.3 Boolean Coercion(Truthy & Falsy Values)- Falsy Values (Evaluated as false)
"" , 0 , -0 , NaN , null , undefined , false
console.log(Boolean("")); // false (Falsy) console.log(Boolean(0)); // false (Falsy) console.log(Boolean(-0)); // false (Falsy) console.log(Boolean(NaN)); // false (Falsy) console.log(Boolean(null)); // false (Falsy) console.log(Boolean(undefined)); // false (Falsy) console.log(Boolean(false)); // false (Falsy) console.log(Boolean("Hello")); // true (Truthy) console.log(Boolean(" ")); // true (Truthy) console.log(Boolean(1)); // true (Truthy) console.log(Boolean(true)); // true (Truthy) console.log(Boolean([])); // true (Truthy) console.log(Boolean({})); // true (Truthy)- Falsy Values (Evaluated as false)
1.4 Logical Operators & Short-Circuiting- Logical operators (
&&,||) return the actual value, not just true or false. !return just true or false.
console.log(0 || "Hello"); // "Hello" console.log("" || "Default"); // "Default" console.log(null || 42); // 42 console.log(false || true); // true console.log(5 && "Hello"); // "Hello" console.log("" && "World"); // "" (Falsy, stops evaluation) console.log(0 && 10); // 0 (Falsy) console.log(true && false); // false console.log(!"Hello"); // false console.log(!0); // true console.log(!null); // true- Logical operators (
1.5 Weird Coercion Casesconsole.log("5" + 2); // "52" (Concatenation) // + prefers string over number console.log("5" - 2); // 3 // other operations prefers number over string console.log(null + 1); // 1 (null is converted to 0) console.log(undefined + 1); // NaN (undefined becomes NaN) console.log(true + true); // 2 (true → 1) console.log(false + 1); // 1 (false → 0) console.log(true * 10); // 10
Type Coercion in Comparisons
- The == operator converts types before comparing.
strict equality ===, which does not coerce types.
console.log(5 == "5"); // true (String "5" converted to Number) console.log(0 == ""); // true (Both converted to 0) console.log(null == undefined); // true (Special case) console.log(false == 0); // true console.log(NaN == NaN); // false console.log(5 === "5"); // false (Different types) console.log(0 === ""); // false (Different types) console.log(null === undefined); // false console.log(false === 0); // false
DataType in Memory
- JavaScript handles data types in memory using two primary storage locations:
- 1.
Stack(for primitive types) - 2.
Heap(for reference types)
- 1.
Stack Memory (For Primitive Types)
- Stack memory is used for storing primitive values and function call execution.
- These datatypes are immutable (cannot be modified) and stored directly in memory.
- It operates on LIFO (Last In, First Out) order.
- Data stored in the stack is fast and automatically managed (garbage collected).
let x = 10; let y = x; // Copying value, NOT reference console.log(x); // 10 console.log(y); // 10 y = 20; // Changing y does NOT affect x console.log(x); // 10 console.log(y); // 20 x = 12; // Changing x does NOT affect y console.log(x); // 12 console.log(y); // 20Heap Memory (For Reference Types)
- Heap memory is used for storing objects, arrays, and functions.
- Objects are mutable (modifications affect all references to the object).
- These hold references to memory locations instead of storing values directly.
- Reference types store a memory address (reference) in the stack, while the actual value is stored in the heap.
- Data stored in the heap is slower and manually managed (garbage collected).
let obj1 = {name: "Alice" }; let obj2 = obj1; // Both variables point to the same memory location obj2.name = "Bob"; // Changing obj2 affects obj1 console.log(obj1.name); // Output: "Bob" console.log(obj2.name); // Output: "Bob" obj1.name = "Sam"; // And changing obj1 affects obj2 console.log(obj1.name); // Output: "Sam" console.log(obj2.name); // Output: "Sam"
5.Literals
- Array Literals (
[1, 2, 3]) Boolean Literals (
true,false)Numeric Literals (5 , 3.14 , 0xFF, 34n)
- BigInt (
3421n) - Floating-point (
3.2352 , 23.5321)
- BigInt (
Object Literals (key: value)
normal object literals
const name = "Alice"; const age = 25; const greet = function() { console.log(`Hello, my name is ${name}`); } // Old Syntax const person = { name: name, age: age, greet: greet }; console.log(person); // {name: 'Alice', age: 25, greet: [Function: greet] } person.greet(); // Hello, my name is AliceEnhanced Object Literals ( foo: foo )
const name = "Alice"; const age = 25; const greet = function() { console.log(`Hello, my name is ${name}`); } const person = { name, age, greet }; console.log(person); // { name: 'Alice', age: 25, greet: [Function: greet] } person.greet(); // Hello, my name is Aliceconst person = { name: "Alice", greet: function() { console.log(`Hello, my name is ${this.name}`); } }; person.greet(); // Output: Hello, my name is AliceString Literals ('Hello', "World")
- template literals (``)
- Expression Interpolation (
${}) - tagged template literals
${} with some strings
Using special characters in strings
0Null BytebBackspacefForm FeednNew LinerCarriage ReturntTabvVertical tab'Apostrophe or single quote"Double quoteBackslash characterXXXLatin-1 encoding octal digits (251 -- copyright symbol)xXXLatin-1 encoding hexadecimal digits (xA9 -- copyright symbol)uXXXXUnicode character hexadecimal digits (u00A9 -- copyright symbol)u{XXXXX}Unicode code point escapesUndefined Literal (undefined)Null Literal (null)Regular Expression Literals (/abc/)
2.Control flow and error handling
1.Block statement
- A group of zero or more statements enclosed in
{}. - Used to group multiple statements together.
2.Conditional statements
- if: Executes a block of code if a logical condition is true.
- if...else: Executes one block of code if the logical condition is true and another block if it is false.
- if...else if...else: Chain multiple conditions.
- Truthy value: all values are Truthy except (
false,0,"",null,undefined, andNaN) are Falsy. - switch: switch case break executes one out of many blocks based on a matching case.
- Break: It is used to or terminate a switch case or exit loops (for, while, do...while).
3.Exception handling statements
- throw: Used to throw a custom error.
- try...catch: Tests a block of code for errors and handles them.
- finally: Defines code that runs regardless of an error.
- Nested try...catch
- Utilizing Error objects
4.Break statement
- It is used to or terminate a switch case or exit loops (for, while, do...while).
- Exits from a loop or switch statement.
- Can be used to terminate a loop prematurely based on a condition.
3.Loops and iteration
- There are many kinds of loops,but they all essentially do the same thing.
- they repeat an action some number of times.
- they have different ways to determine the start and end points of the loop.
1. for statement
- A loop that repeats until a specified condition evaluates to false.
- for (initialization; condition; afterthought) {statements}
2. do...while statement
- A loop that executes at least once and then repeats while a condition is true.
- do {statements} while (condition);
3. while statement
- A loop that repeats as long as a condition is true.
- while (condition) {statements};
- Avoid infinite loops while using 'while' condition.
4. labeled statement
- A statement with a label that can be used with break or continue to control flow.
- label : {statements};
5. break statement
- Exits from a while, do-while, for loop or a switch statement.
6. continue statement
- Skips the current iteration of a loop and continues with the next iteration.
- The continue statement can be used to restart a while, do-while, for, or label statement.
7. for...in statement (for Object's key)
- A loop that iterates over the keys of an object.
- for (variable in object) {statements};
- for (const i in objectName) {};
- for (let i in objectName) {};
8. for...of statement (for Array's, Map's & Set's values )
- A loop that iterates over the values of an iterable object (e.g., Array, Map, Set).
- or (variable of iterable) {statements};
- or (const i of objectName) {};
- or (let i of objectName) {};
9. Iteration with Array methods
- map(): Creates a new array with the results of calling a provided function on every element.
- forEach(): Executes a provided function once for each element in the array.
- filter(): Creates a new array with all elements that pass the test implemented by the provided function.
- reduce(): Applies a function against an accumulator and each element in the array (from left to right) to reduce it to a single value.
- some(): Tests whether at least one element in the array passes the test implemented by the provided function.
- every(): Tests whether all elements in the array pass the test implemented by the provided function.
10. Iterators and Generators
- Iterator: An object that defines a sequence and potentially return a value upon completion.
- Generator: A function that can return multiple values lazily using the yield keyword.
4.Functions
- A function in JavaScript is a reusable block of code designed to perform a specific task.
1.Defining functions
Defining a function does not execute it.
Defining it names the function and specifies what to do when the function is called.
Function Declaration
- A function is defined using the
functionkeyword withreturnkeyword. - function functionName(parameters) {statements return value;}
- A function is defined using the
- Function Expression
- Functions can be defined inside expressions and assigned to variables.
const functionName = function (parameters) {statements return value;}- Anonymous Functions: Functions without a name, typically used as arguments or passed around.
- Named Function Expressions: Functions defined inside expressions with an explicit name.
- Self-Invoking Functions
IIFE: Functions that execute immediately after being defined.
- Named Function Expression
- Functions defined inside expressions with a name.
const functionName = function actualName(parameters) {statements return value;}- Similar to a function expression, but the function has an internal name, useful for recursion and debugging.
- Function Constructor
- Functions can be dynamically created using the
Functionconstructor. const functionName = new Function (parameters) {statements return value;}- not recommended for performance and security reasons
- Functions can be dynamically created using the
- Generator Functions
- Functions that can yield multiple values over time using the
yieldkeyword. function* numberGenerator() {yield value1;... yield valueN;}const gen = numberGenerator(); // Create generator instanceconsole.log(gen.next().value); // value1console.log(gen.next().value); // valueNconsole.log(gen.next().value); // undefinedconsole.log(gen.next().done); // true (no more values)
- Functions that can yield multiple values over time using the
2.Calling functions
Calling the function actually performs the specified actions with the indicated parameters.
Invoking Functions
- Functions are invoked using their name followed by parentheses
().
- Functions are invoked using their name followed by parentheses
- Arguments
- Arguments can be passed to functions when calling them.
- Function hoisting
- hoisting is JavaScript’s behavior of moving function and variable declarations to the top of their scope before execution.
- Hoisting of Function Declarations
- Function declarations are hoisted to the top of their scope and can be called before the function is defined.
- Hoisting of Function Expressions
- Function expressions are not hoisted like function declarations and cannot be called before they are assigned.
- Recursion
- Recursion is a programming technique where a function calls itself to solve a problem by breaking it down into smaller subproblems.
- It continues until it reaches a base case that stops the recursion.
- Immediately Invoked Function Expressions
(IIFE)- Anonymous IIFE: An anonymous function that is defined and executed immediately.
- Named IIFE: A named function that is immediately invoked.
3.Function scopes and closures
- Function Scope
- Variables declared inside a function are scoped to that function.
- Global Scope
- Variables declared outside functions are in the global scope.
- Lexical Scoping
- Functions remember the scope in which they were created, even when executed outside that scope.
- Closures
- Functions that capture and maintain references to their outer scope.
- Nested Functions
- Functions defined inside other functions, creating inner scopes.
- Multiply-nested functions
- A multiply nested function is a function inside another function, creating multiple levels of nesting.
- Name conflict
- A name conflict occurs when two variables or functions have the same name in overlapping scopes, leading to unexpected behavior or errors.
4.Using the arguments object
- arguments Object
- An array-like object available in non-arrow functions containing all arguments passed to the function.
- Accessing arguments in non-arrow functions
- arguments is available in regular functions, but not in arrow functions.
- Arguments Length
- The arguments object has a length property that indicates the number of arguments passed.
- Accessing Specific Arguments
- Accessing specific arguments by index via arguments[i].
- Spread Operator
- while giving array argument we can use Spread (... Operator) in Arguments.
5.Function parameters
- Formal Parameters
- Variables declared in the function definition to accept input.
- Destructuring Parameters
- Extract values from objects or arrays directly within the function parameters.
- Rest Parameters (... Operator)
- Allows collecting all remaining arguments into an array.
- while giving array argument it is called Spread (... Operator in Arguments)
- Default Parameters
- Allows setting default values for parameters if no argument is passed.
- Required (Positional) Parameters
- Must be passed when calling the function.
- Optional Parameters
- Parameters that are optional and can be omitted when calling the function.
6.Arrow functions
- Arrow Function Syntax
- A more concise way to write functions using
()=>{}.
- A more concise way to write functions using
- Concise Body vs. Block Body
- Arrow functions can have a concise body that returns a value
- or a block body with explicit return.
- Implicit Return
- In a single expression, the result is returned implicitly without the
returnkeyword. - const add = (a, b) => a + b;
- In a single expression, the result is returned implicitly without the
- Explicit Return
- Arrow functions return a value using the return keyword in {}.
- Shorter functions Single Expression
- A function with a single expression and an implicit return.
- Lexical this Binding
- Arrow functions do not have their own this;
- they inherit this from the surrounding context.
7. The this keyword
- Global this
- Refers to the global object (window in browsers) when used outside of any function or object.
- Object this
- Inside an object method, this refers to the object itself.
- Function this
- In regular functions, this refers to the object that invoked the function.
- Arrow Function this
- Arrow functions do not have their own this; they inherit it from the lexical scope.
- this in Class Methods
- In class methods, this refers to the instance of the class.
8. Higher-order functions
- Function as Arguments
- Functions that accept other functions as parameters.
- Function as Return Values
- Functions that return other functions.
- Callbacks
- Functions that are passed as arguments to other functions and invoked later.
- Array Methods that Accept Functions
- Methods like
map(),filter(), andreduce()that take a function as an argument.
- Methods like
9. call(), apply(), and bind() methods
- call()
- Invokes a function with a specified this and arguments passed individually.
- apply()
- Similar to call(), but arguments are passed as an array.
- bind()
- Returns a new function with a specified this value and arguments pre-set.
10. Function overloading
- Simulating Overloading
- JavaScript does not support traditional overloading, but functions can behave differently based on the number of arguments passed.
11. Debouncing and Throttling Functions
- Debouncing
- A technique used to limit the rate at which a function is invoked, particularly in event handling.
- Throttling
- Ensures that a function is invoked at most once in a given time period.
12. Function caching/memoization
- Memoization
- Caching the results of function calls based on the arguments passed, so repeated calls with the same arguments return the cached result.
5.Expressions and operators
1.Expressions & operators
- Expression
- Any valid unit of code that resolves/evaluates to a value.
- It can consist of variables, operators, function calls, and other elements, and when evaluated, it returns a result (or value).
- Operators
- Operators are special symbols that perform operations on operands (values or variables).
- Evaluation of Expressions
- JavaScript evaluates expressions in a specific order, considering operator precedence and associativity.
- Order of Evaluation
- Parentheses (), Exponentiation **, Unary Operators
(+, -, ++, --, !, ~), - Multiplication *, Division /, Modulo %, Bitwise Shift Operators
(<<, >>, >>>), - Addition +, Subtraction -, Relational Operators
(<, <=, >, >=), Equality Operators(==, ===, !=, !==), - Bitwise AND
&, Bitwise XOR^, Bitwise OR|, Logical AND&&, Logical OR||, - Conditional
(? :), Assignment Operators(=, +=, -=, *=, /=, etc.), Comma Operator ,
- Parentheses (), Exponentiation **, Unary Operators
2.Assignment Operators
=- Assigns a value to a variable.
+=,-=,*=,/=,%=,**=- Compound assignment operators that combine an arithmetic operation with assignment.
<<=,>>=,>>>=- left shift assignment, right shift assignment, Unsigned right shift assignment
&=,^=,|=- Bitwise AND assignment, Bitwise XOR assignment, Bitwise OR assignment
&&=,||=- Logical AND assignment, Logical OR assignment
3.Comparison Operators
==,===,!=,!==- Compare two values for equality or inequality (loose vs. strict comparison).
>,<,>=,<=- Compare two values for greater than, less than, greater than or equal, and less than or equal.
4.Arithmetic Operators
+,-,*,/,%,**- Perform mathematical calculations: addition, subtraction, multiplication, division, modulus, and exponentiation.
5.Bitwise Operators
&,|,^,~,<<,>>,>>>- Perform bit-level operations (AND, OR, XOR, NOT, shift left, shift right, unsigned shift right).
6.Logical Operators
&&(AND),||(OR),!(NOT)- Perform logical operations on boolean values.
??- Returns the right-hand operand when the left-hand operand is
nullorundefined.
- Returns the right-hand operand when the left-hand operand is
7.BigInt Operators
- You can create a BigInt by appending n to an integer or using the BigInt() constructor.
- BigInt and Number types cannot be mixed directly in mathematical operations.
const a = 1n + 2n; // 3nconst b = 1n / 2n; // 0nconst c = 40000000000000000n >> 2n; // 10000000000000000nconst d = 8n >>> 2n; // TypeError: BigInts have no unsigned right shift,use >> instead- You can not mix BigInt and other types
const a = 1n + 2; // TypeError: Cannot mix BigInt and other typesconst a = Number(1n) + 2; // 3const b = 1n + BigInt(2); // 3n- You can compare BigInts with numbers.
const a = 1n > 2; // falseconst b = 3 > 2n; // true
8.String Operators
- Concatenation Operator (
+) - Addition Assignment Operator (
+=) - Comparison Operators (
===, !==, <, >, <=, >=) - String Length Property (
.length) - String Accessor (
[]) - Template Literals (
${expression}) - String Repetition Operator (
repeat())
9.Conditional (Ternary) Operator
condition ? expr1 : expr2- Returns one of two expressions based on a condition.
10.Comma Operator
,- Evaluates multiple expressions and returns the result of the last one.
- for (let i = 0, j = 9; i <= j; i++, j--)
- Here we are talking about the comma after i++
11.Unary Operators
- An unary operation is an operation with only one operand.
+,-, pre++, post++, pre--, post--,!,~- Perform operations on a single operand (e.g., negation, increment, decrement, logical NOT, bitwise NOT).
- console.log(+x); // Output: 12 where x is 12
- console.log(-y); // Output: -10 where y is 10
- console.log(++a); // Output: 6 (pre-increment) where a is 5
- console.log(a++); // Output: 5 (post-increment) where a is 5
- console.log(--b); // Output: 4 (pre-increment) where b is 5
- console.log(b--); // Output: 5 (post-increment) where b is 5
- console.log(!isTrue); // Output: true where isTrue is 'false'
- console.log(~num); // Output: -6 // Inverts bits (x → -(x+1))
typeof- Returns a string indicating the type of a variable or expression.
delete- let obj = { name: "Alice" };
- delete obj.name;
- console.log(obj); // Output: {}
void- console.log(void 0); // Output: undefined
- void 1234 evaluates 1234 but discards it.
- The result is always undefined.
12.Relational operators
- A relational operator compares its operands and returns a Boolean value based on whether the comparison is true.
in- The in operator returns true if the specified property is in the specified object.
- The syntax is: 'propNameOrIndexNumber' in 'objectName'
instanceof- similar to typeof but it needs two operands.
- The instanceof operator returns true if the specified object is of the specified object type.
- The syntax is: 'object' instanceof 'objectType'
- Checks if an object is an instance of a particular class or constructor function.
13.Basic expressions
- All operators eventually operate on one or more basic expressions.
thisthis["propertyName"];this.propertyName;- Grouping Operator
() // addition before multiplication(a + b) * c // 9- Property accessor
.,[] - The property accessor syntax gets property values on objects, using either dot notation or bracket notation.
object.property;object["property"];- Optional chaining
?. maybeObject?.property;maybeObject?.[property];maybeFunction?.();- new
- You can use the new operator to create an instance of a user-defined object type or of one of the built-in object types.
- Use new as follows:
const objectName = new ObjectType(param1, param2, //...//, paramN);- super
- The super keyword is used to call functions on an object's parent.
- It is useful with classes to call the parent constructor, for example.
super(args); // calls the parent constructor.super.functionOnParent(args);
14.Destructuring Assignment
- Array Destructuring
- Allows extracting values from arrays into individual variables.
- Object Destructuring
- Allows extracting values from objects into individual variables.
15.Spread & Rest Operators
- The spread operator is used to expand or spread elements of an array or object into individual elements.
- The rest operator is used to collect multiple elements into a single variable (usually an array or object) in function arguments or destructuring.
6.Numbers and strings
1. Numbers
- Integer (Whole Numbers)
- Positive Integers
- Negative Integers
- Zero
- Floating-Point (Decimal Numbers)
- Precision Limitations (IEEE 754 Standard)
- Rounding Errors
- Scientific Notation
- BigInt (Arbitrary Precision Integers)
- Declaring BigInt (n suffix)
- Operations with BigInt
- Limitations (Cannot mix with Number type)
- NaN (Not-a-Number)
- Causes of NaN
- Checking for NaN (isNaN() and Number.isNaN())
- NaN is not equal to NaN as they are different objects.
- Infinity (Positive & Negative)
- Infinity (Positive)
- -Infinity (Negative)
- Operations resulting in Infinity
- Decimal numbers)
- Normal Number System
- Binary (Base 2 Numbers - 0b prefix)
- Declaring Binary Numbers
- Converting Binary to Decimal
- Use Cases in Bitwise Operations
- Octal (Base 8 Numbers - 0o prefix)
- Declaring Octal Numbers
- Converting Octal to Decimal
- Legacy Support (Older Syntax Issues)
- Hexadecimal (Base 16 Numbers - 0x prefix)
- Declaring Hex Numbers
- Converting Hex to Decimal
- Use Cases (Color Codes, Memory Addresses)
2. Number object
- Properties
- const biggestNum =
Number.MAX_VALUE; - const smallestNum =
Number.MIN_VALUE; - const infiniteNum =
Number.POSITIVE_INFINITY; - const negInfiniteNum =
Number.NEGATIVE_INFINITY; - const notANum =
Number.NaN; - const Epsilon =
Number.EPSILON; - const MinSafeInteger =
Number.MIN_SAFE_INTEGER; - const MaxSafeInteger =
Number.MAX_SAFE_INTEGER;
- const biggestNum =
- Methods
Number.parseFloat(): Parses a string argument and returns a floating point number. Same as the global parseFloat() function.Number.parseInt(): Parses a string argument and returns an integer of the specified radix or base. Same as the global parseInt() function.Number.isFinite(): Determines whether the passed value is a finite number.Number.isInteger(): Determines whether the passed value is an integer.Number.isNaN(): Determines whether the passed value is NaN. More robust version of the original global isNaN().Number.isSafeInteger():Determines whether the provided value is a number that is a safe integer.toExponential(): Returns a string representing the number in exponential notation.toFixed(): Returns a string representing the number in fixed-point notation.toPrecision(): Returns a string representing the number to a specified precision in fixed-point notation.
3. Math object
- The 'Math' object is a built-in JavaScript object that provides mathematical functions and constants.
- It has no constructor and is accessed directly using
Math.propertyorMath.method(). - Math Constants
Math.PI→ π (Pi value)Math.E→ Euler's numberMath.LN2→ Natural logarithm of 2Math.LN10→ Natural logarithm of 10Math.LOG2E→ Base-2 logarithm of Euler's numberMath.LOG10E→ Base-10 logarithm of Euler's numberMath.SQRT2→ Square root of 2Math.SQRT1_2→ Square root of 1/2
- Rounding Methods
Math.round(x)→ Rounds to the nearest integerMath.ceil(x)→ Rounds upMath.floor(x)→ Rounds downMath.trunc(x)→ Removes the decimal part
- Power and Root Methods
Math.pow(x, y)→ Returnsxraised to the poweryMath.sqrt(x)→ Returns the square root ofxMath.cbrt(x)→ Returns the cube root ofxMath.exp(x)→ Returnse^x(Euler’s number raised tox)
- Logarithm Methods
Math.log(x)→ Natural logarithm (basee)Math.log2(x)→ Logarithm base 2Math.log10(x)→ Logarithm base 10
- Trigonometric Methods
Math.sin(x)→ Sine ofx(radians)Math.cos(x)→ Cosine ofx(radians)Math.tan(x)→ Tangent ofx(radians)Math.asin(x)→ Arcsine ofxMath.acos(x)→ Arccosine ofxMath.atan(x)→ Arctangent ofxMath.atan2(y, x)→ Arctangent ofy/x
- Random and Min/Max Methods
Math.random()→ Returns a random number between0and1Math.min(a, b, c, ...)→ Returns the smallest valueMath.max(a, b, c, ...)→ Returns the largest value
- Absolute and Sign Methods
Math.abs(x)→ Returns the absolute value ofxMath.sign(x)→ Returns1(positive),-1(negative), or0
4. BigInts
- BigInt is a special numeric type that allows you to work with arbitrary-precision integers,
- meaning numbers larger than
Number.MAX_SAFE_INTEGER (2^53 - 1). - It is created by adding an
nsuffix to a number or using theBigInt()constructor. - Creating BigInt
- let big1 = 1234567890123456789034560n; → Using
nsuffix - let big2 = BigInt("12345678901234567890"); → Using
BigInt()constructor
- let big1 = 1234567890123456789034560n; → Using
- BigInt Arithmetic Operators
+→ Addition (big1 + big2)-→ Subtraction (big1 - big2)*→ Multiplication (big1 * big2)/→ Division (Rounding down, no decimals) (big1 / big2)%→ Modulus (big1 % big2)**→ Exponentiation (big1 ** 2n)
- BigInt Comparison Operators
==→ Loose equality (big1 == 123n)===→ Strict equality (BigInt and Number are not the same type)<,>,<=,>=→ Comparisons work normally
- BigInt and Type Conversion
BigInt()→ Converts a number or string to BigIntNumber(bigIntValue)→ Converts BigInt to Number (may lose precision)
5. Strings
- JavaScript's String type is used to represent textual data
- String Literals
- Single-quoted (
'text') - Double-quoted (
"text") - Template Literals (
`text`) - Supports multi-line strings and embedded expressions using
${expression}.
- Single-quoted (
- Hexadecimal Escape Sequences
- Represent characters using hexadecimal values (
xHHformat). His a hex digit (0-9, A-F).- Example:
x41→ Represents "A" (U+0041in Unicode).
- Represent characters using hexadecimal values (
- Unicode Escape Sequences
- Use
uHHHHformat (H= hex digit, 4 digits required). - Represent Unicode characters.
- Example:
u03A9→ Represents "Ω" (Greek Omega).
- Use
- Unicode Code Point Escapes (ES6)
- Use
u{HHHHH}format (allows more than 4 hex digits). - Used for characters outside the Basic Multilingual Plane (BMP).
- Example:
u{1F600}→ Represents "😀" (Emoji U+1F600).
- Use
6. String object and Methods
- You can call methods directly on a string value:
- Query Methods (Get Character or Character Code)
- Retrieve characters or character codes at a specific index in a string.
at(index)→ Returns the character at the given index (supports negative indices).charAt(index)→ Returns the character at the specified index.charCodeAt(index)→ Returns the UTF-16 code unit of the character at the index.codePointAt(index)→ Returns the Unicode code point at the specified index.
- Search Methods (Find Substrings or Patterns)
- Find occurrences of a substring or check if a substring exists.
indexOf(substring)→ Returns the index of the first occurrence.lastIndexOf(substring)→ Returns the index of the last occurrence.startsWith(substring)→ Checks if the string starts with the given substring.endsWith(substring)→ Checks if the string ends with the given substring.includes(substring)→ Checks if the string contains the given substring.match(regex)→ Returns matches of a regular expression.matchAll(regex)→ Returns an iterator of all regex matches.search(regex)→ Returns the index of the first match for a regex.
- Composition Methods (Combine Strings)
- Combine strings into a longer string.
padStart(length, padString)→ Pads the string at the beginning.padEnd(length, padString)→ Pads the string at the end.concat(string1, string2, ...)→ Concatenates strings.repeat(count)→ Repeats the stringcounttimes.
- Decomposition Methods (Break Strings into Parts)
- Split a string into smaller substrings.
split(separator)→ Splits the string into an array of substrings.slice(start, end)→ Extracts a section of the string.substring(start, end)→ Returns a substring between indices.substr(start, length)→ Returns a substring of given length (deprecated).trim()→ Removes whitespace from both ends.trimStart()→ Removes whitespace from the start.trimEnd()→ Removes whitespace from the end.
- Transformation Methods (Modify Strings)
- Return a new string based on the existing string.
toLowerCase()→ Converts to lowercase.toUpperCase()→ Converts to uppercase.toLocaleLowerCase()→ Locale-aware lowercase conversion.toLocaleUpperCase()→ Locale-aware uppercase conversion.normalize(form)→ Returns Unicode normalization form.toWellFormed()→ Ensures valid UTF-16 encoding.
- Additional String-Related Objects
- JavaScript provides additional objects for string manipulation:
- RegExp → Used for pattern matching with regular expressions.
- Intl → Provides internationalization support for locale-aware string operations.
7. Template literals
- Template literals are string literals allowing embedded expressions.
You can use multi-line strings and string interpolation features with them.
Multi-line Strings
console.log(`string text line 1
string text line 2`);- Embedded expressions
const name = "Alice";
const age = 25;
const message = `My name is ${name} and I am ${age} years old.`;7.Representing dates & times
- The 'Date' object in JavaScript is used to work with dates and times.
- It allows you to create, format, and manipulate dates.
- It does not have any properties.
1. Creating, getting & setting Date Ojbect
- Creating Date Objects
new Date()→ Returns the current date and time.new Date(year, month, day, hours, minutes, seconds, milliseconds)→ Creates a date with specific values.new Date(milliseconds)→ Creates a date from milliseconds since January 1, 1970 (Unix Epoch).new Date(dateString)→ Parses a date string.
- Getting Date Components
getFullYear()→ Returns the 4-digit year.getMonth()→ Returns the month (0-11, where 0 = January).getDate()→ Returns the day of the month (1-31).getDay()→ Returns the day of the week (0-6, where 0 = Sunday).getHours()→ Returns the hours (0-23).getMinutes()→ Returns the minutes (0-59).getSeconds()→ Returns the seconds (0-59).getMilliseconds()→ Returns the milliseconds (0-999).getTime()→ Returns the timestamp (milliseconds since 1970).getTimezoneOffset()→ Returns the difference (in minutes) between UTC and local time.
- Setting Date Components
setFullYear(year, month, day)→ Sets the year.setMonth(month, day)→ Sets the month (0-11).setDate(day)→ Sets the day of the month.setHours(hours, minutes, seconds, milliseconds)→ Sets the hours.setMinutes(minutes, seconds, milliseconds)→ Sets the minutes.setSeconds(seconds, milliseconds)→ Sets the seconds.setMilliseconds(milliseconds)→ Sets the milliseconds.setTime(milliseconds)→ Sets the time using milliseconds.
2. Formatting, Comparing & Parsing Date Ojbect
- Formatting Dates
toDateString()→ Returns a readable date string.toTimeString()→ Returns a readable time string.toISOString()→ Returns the date in ISO 8601 format (YYYY-MM-DDTHH:mm:ss.sssZ).toUTCString()→ Returns the date in UTC format.toLocaleDateString(locale, options)→ Returns a localized date string.toLocaleTimeString(locale, options)→ Returns a localized time string.toLocaleString(locale, options)→ Returns a localized date and time string.
- Date Comparison & Operations
date1 > date2→ Checks if date1 is later than date2.date1 < date2→ Checks if date1 is earlier than date2.date1.getTime() === date2.getTime()→ Checks if two dates are equal.date1 - date2→ Returns the difference between two dates in milliseconds.
- Parsing Dates
Date.parse(dateString)→ Converts a date string into milliseconds.Date.UTC(year, month, day, hours, minutes, seconds, milliseconds)→ Returns the UTC timestamp.
- Key Features of the Date Object
- Represents dates and times.
- Supports creating and modifying date values.
- Allows formatting dates in various ways.
- Provides methods for comparing and manipulating time differences.
8.Regular expressions (RegExp)
- Regular expressions are patterns used to match character combinations in strings.
1. Creating Regular Expressions
- we can construct a regular expression in one of two ways:
- Literal Notation:
/pattern/flags - RegExp Constructor:
new RegExp("pattern", "flags")
2. Writing a regular expression pattern
- Character classes
[xyz]→ Matches any one of the characters x, y, or z.[^xyz]→ Matches any character except x, y, or z..→ Matches any character except newline (n).d→ Matches any digit (0-9).D→ Matches any non-digit character.w→ Matches any word character (a-z, A-Z, 0-9, _).W→ Matches any non-word character.s→ Matches any whitespace character (space, tab, newline).S→ Matches any non-whitespace character.t→ Matches a tab character.r→ Matches a carriage return.n→ Matches a newline character.v→ Matches a vertical tab.f→ Matches a form feed.[b]→ Matches a backspace character.0→ Matches a null character (x00).cX→ Matches a control character, where X is a letter (cA for Ctrl+A).xhh→ Matches an ASCII character using hexadecimal (x41 for A).uhhhh→ Matches a Unicode character (u03A9 for Ω).u{hhhh}→ Matches a Unicode code point (u{1F600} for 😀).x|y→ Matches either x or y.
- Assertions
^→ Matches the start of a string.$→ Matches the end of a string.b→ Matches a word boundary (between a word and a non-word character).B→ Matches a non-word boundary (between two word characters or two non-word characters).x(?=y)→ Positive lookahead: Matches x only if followed by y.x(?!y)→ Negative lookahead: Matches x only if not followed by y.(?<=y)x→ Positive lookbehind: Matches x only if preceded by y.(?<!y)x→ Negative lookbehind: Matches x only if not preceded by y.
- Groups and backreferences
(x)→ Capturing Group: Captures the matched text of x for later use (e.g., backreferences).(?< Name>x)→ Named Capturing Group: Captures x and assigns it the name Name, allowing you to refer to it by name.(?:x)→ Non-Capturing Group: Groups x without capturing it for backreferences.n→ Matches a newline character (n).k< Name>→ Backreference by Name: Matches the same text as the named capturing group Name.
- Quantifiers
x*→ Matches 0 or more occurrences of x (greedy).x+→ Matches 1 or more occurrences of x (greedy).x?→ Matches 0 or 1 occurrence of x (optional).x{n}→ Matches exactly n occurrences of x.x{n,}→ Matches n or more occurrences of x.x{n,m}→ Matches between n and m occurrences of x.
- Escaping
(Backslash) → Used to escape special characters, allowing them to be treated literally.- Special Characters that Need Escaping:
., *, +, ?, ^, $, (, ), {, }, [, ], |, , /, :, &
- Using parentheses
- Parentheses around any part of the regular expression pattern causes that part of the matched substring to be remembered.
- Once remembered, the substring can be recalled for other use.
3. Using regular expressions in JavaScript
- Regular expressions are used with the RegExp methods
test()andexec() - with the String methods
match(),matchAll(),replace(),replaceAll(),search()andsplit(). - Methods
test(): Tests for a match in a string. It returns true or false.exec(): Executes a search for a match in a string. It returns an array of information or null on a mismatch.match(): Returns an array containing all of the matches, including capturing groups, or null if no match is found.matchAll(): Returns an iterator containing all of the matches, including capturing groups.replace(): Executes a search for a match in a string, and replaces the matched substring with a replacement substring.replaceAll(): Executes a search for all matches in a string, and replaces the matched substrings with a replacement substring.search(): Tests for a match in a string. It returns the index of the match, or -1 if the search fails.split(): Uses a regular expression or a fixed string to break a string into an array of substrings.
- Flags (Modifiers)
- Flags modify how the regular expression behaves.
d→ Generate indices for substring matches.g→ Global search (match all occurrences)i→ Case-insensitive searchm→ Makes ^ and $ match the start and end of each line instead of those of the entire string.s→ Dot (.) matches newline (n)u→ "Unicode"; treat a pattern as a sequence of Unicode code points.v→ An upgrade to the u mode with more Unicode features.y→ Sticky search (match at the exact position)
- Common Regex Patterns
.→ Matches any character except newline^→ Matches the start of a string$→ Matches the end of a stringd→ Matches any digit (0-9)D→ Matches any non-digitw→ Matches any word character (a-z, A-Z, 0-9, _)W→ Matches any non-word characters→ Matches any whitespaceS→ Matches any non-whitespaceb→ Matches a word boundaryB→ Matches a non-word boundary
9.Indexed collections
1. Creating Array like objects
- This includes arrays and array-like constructs such as Array objects and TypedArray objects.
- An array is an ordered list of values that you refer to with a name and an index.
Creating an array using
new Array&Arrayconst arr1 = new Array(element0, element1,...., elementN); const arr2 = Array(element0, element1,...., elementN); // array literal / array initializer: const arr3 = [element0, element1,...., elementN];To create an array with non-zero length, but without any items
- any of the following can be used:
const arr1 = new Array(arrayLength); const arr2 = Array(arrayLength); const arr3 = []; arr3.length = arrayLength; // In the above code, arrayLength must be a Number. Otherwise, // an array with a single element (the provided value) will be created. const singleArray = Array.of(9.8); // singleArray contains only one element 9.8Referring to array elements
- Because elements are also properties, you can access them using property accessors.
const arr = ["one", "two", "three"]; arr[2]; // three arr["length"]; // 3Populating an array
- You can populate an array by assigning values to its elements.
const emp = []; emp[0] = "Casey Jones"; emp[1] = "Phil Lesh"; emp[2] = "August West";- If you supply a non-integer value to the array operator in the code above,
- a property will be created in the object representing the array, instead of an array element.
const arr = []; arr [3.4] = "Oranges"; console.log(arr.length); // 0 console.log(Object.hasOwn(arr, 3.4)); // true- You can also populate an array when you create it
const myArray = new Array ("Hello", myVar, 3.14159); const myArray = ["Mango", "Apple", "Orange"];Understanding length
- JavaScript's arrays actually store their elements as standard object properties,
- using the array index as the property name.
const dogs = []; dogs[20] = ["Kimi"]; console.log(dogs.length); // 21- Writing a value that is shorter than the number of stored items truncates the array. Writing 0 empties it entirely:
const cats = ["Dusty", "Misty", "Twiggy"]; console.log(cats.length); // 3 cats.length = 2; console.log(cats); // ['Dusty', 'Misty'] - Twiggy has been removed cats.length = 0; console.log(cats); // []; the cats array is empty cats.length = 3; console.log(cats); // [<3 empty items>]Iterating over arrays
- iterate over the values of an array
const colors = ["red", "green", "blue"]; for (let i = 0; i < colors.length; i++) {console.log(colors[i]); } // Logs each element, including undefined for the empty slot const colors = ["red", "green", , "blue"]; colors.forEach((color) => console.log(color)); // red// green// blue- the elements of an array that are omitted when the array is defined are not listed when iterating by forEach.
2. Array methods
Array methods
- Mutator Methods (Modify Original Array)
push()- Adds elements to the end of an array.pop()- Removes and returns the last element.shift()- Removes and returns the first element.unshift()- Adds elements to the beginning of an array.splice()- Adds/removes elements at a specific index.reverse()- Reverses the array in place.sort()- Sorts the elements of an array.fill()- Fills all or part of an array with a static value.copyWithin()- Copies a section of an array within itself.
- Accessor Methods (Return a Value, Do Not Modify)
concat()- Merges two or more arrays.slice()- Returns a shallow copy of a portion of an array.join()- Joins array elements into a string.indexOf()- Returns the first index of a specified element.lastIndexOf()- Returns the last index of a specified element.includes()- Checks if an array contains a specific value.toString()- Converts an array to a string.toLocaleString()- Converts an array to a localized string.
- Iteration Methods (Process Each Element)
forEach()- Executes a function for each element.map()- Creates a new array with transformed elements.filter()- Creates a new array with elements that pass a test.reduce()- Reduces an array to a single value.reduceRight()- Reduces an array to a single value from right to left.some()- Checks if at least one element meets a condition.every()- Checks if all elements meet a condition.find()- Returns the first element that meets a condition.findIndex()- Returns the index of the first matching element.flat()- Flattens nested arrays into a single-level array.flatMap()- Maps and flattens elements in one step.
- Typed Array Methods (For TypedArray Objects)
set()- Sets multiple values in a typed array.subarray()- Returns a subarray from a typed array.
- Other Methods
from()- Creates an array from iterable or array-like objects.isArray()- Checks if a value is an array.of()- Creates a new array from provided arguments.keys()- Returns an iterator of array keys.values()- Returns an iterator of array values.entries()- Returns an iterator of key-value pairs.with()- Returns a new array with a value replaced at a given index (ES2023).
- Mutator Methods (Modify Original Array)
Array transformations
- You can transform back and forth between arrays and other data structures.
Grouping the elements of an array
- The
Object.groupBy()method can be used to group the elements of an array by any common property of it's Object elements. - if the information to group is associated with an object that might change, then you can instead use
Map.groupBy().
- The
Sparse arrays
- Arrays can contain "empty slots", which are not the same as slots filled with the value undefined.
- Empty slots can be created in one of the following ways:
// Array constructor: const a = Array(5); // [ <5 empty items> ] // Consecutive commas in array literal: const b = [1, 2, , , 5]; // [ 1, 2, <2 empty items>, 5 ] // Directly setting a slot with index greater than array.length: const c = [1, 2]; c[4] = 5; // [ 1, 2, <2 empty items>, 5 ] // Elongating an array by directly setting .length: const d = [1, 2]; d.length = 5; // [ 1, 2, <3 empty items> ] // Deleting an element: const e = [1, 2, 3, 4, 5]; delete e[2]; // [ 1, 2, <1 empty item>, 4, 5 ]- In some operations, empty slots behave as if they are filled with undefined.
const arr = [1, 2, , , 5]; // Create a sparse array // Indexed access console.log(arr[2]); // undefined // For...of for (const i of arr) { console.log(i); } // Logs: 1 2 undefined undefined 5 // Spreading const another = [...arr]; // "another" is [ 1, 2, undefined, undefined, 5 ]- But in others (most notably array iteration methods), empty slots are skipped.
const mapped = arr.map((i) => i + 1); // [ 2, 3, <2 empty items>, 6 ] arr.forEach((i) => console.log(i)); // 1 2 5 const filtered = arr.filter(() => true); // [ 1, 2, 5 ] const hasFalsy = arr.some((k) => !k); // false // Property enumeration const keys = Object.keys(arr); // [ '0', '1', '4' ] for (const key in arr) { console.log(key); } // Logs: '0' '1' '4' // Spreading into an object uses property enumeration, not the array's iterator const objectSpread = { ...arr }; // { '0': 1, '1': 2, '4': 5 }Multi-dimensional arrays
- Arrays can be nested, meaning that an array can contain another array as an element.
const a = new Array(4); for (let i = 0; i < 4; i++) { a[i] = new Array(4); for (let j = 0; j < 4; j++) { a[i][j] = [${i}, ${j}]; } }Using arrays to store other properties
- Arrays can also be used like objects, to store related information.
const arr = [1, 2, 3]; arr.property = "value"; console.log(arr.property); // "value"Working with array-like objects
- Some JavaScript objects, such as the NodeList returned by document.getElementsByTagName()
- or the arguments object made available within the body of a function,
- look and behave like arrays on the surface but do not share all of their methods.
- The arguments object provides a length attribute but does not implement array methods like forEach().
- Array methods cannot be called directly on array-like objects.
- But you can call them indirectly using
Function.prototype.call().
function printArguments() { Array.prototype.forEach.call(arguments, (item) => {console.log(item);}); }- Array prototype methods can be used on strings as well,
Array.prototype.forEach.call("a string", (chr) => {console.log(chr);});
10.Keyed collections
1.Map object
Map object
- Map object: A Map object is a key/value map that can iterate its elements in insertion order.
- You can use a for...of loop to return an array of [key, value] for each iteration.
const sayings = new Map(); sayings.set("dog", "woof"); sayings.set("cat", "meow"); sayings.set("elephant", "toot"); sayings.size; // 3 sayings.get("dog"); // woof sayings.get("fox"); // undefined sayings.has("bird"); // false sayings.delete("dog"); sayings.has("dog"); // false for (const [key, value] of sayings) { console.log(`${key} goes ${value}`); } // "cat goes meow" // "elephant goes toot" sayings.clear(); sayings.size; // 0Object vs Map:
- The keys of an Object are strings or symbols, whereas they can be of any value for a Map.
- You can get the size of a Map easily, while you have to manually keep track of size for an Object.
- The iteration of maps is in insertion order of the elements.
- An Object has a prototype, so there are default keys in the map.
- (This can be bypassed using map = Object.create(null).)
When to use Map or Object:
- Use maps over objects when keys are unknown until run time, and when all keys are the same type and all values are the same type.
- Use maps if there is a need to store primitive values as keys because object treats each key as a string whether it's a number value,
- boolean value or any other primitive value.
- Use objects when there is logic that operates on individual elements.
WeakMap object
- A WeakMap is a collection of key/value pairs whose keys must be objects or non-registered symbols,
- with values of any arbitrary JavaScript type, and which does not create strong references to its keys.
2.Set object
Set object
- Set objects are collections of unique values.
- You can iterate its elements in insertion order.
- A value in a Set may only occur once; it is unique in the Set's collection.
const mySet = new Set(); mySet.add(2); mySet.add("some text"); mySet.add("foo"); mySet.add(3); mySet.add(1); mySet.has(1); // true mySet.delete("foo"); mySet.size; // 2 // mySet.clear(); // empty the Set. for (const item of mySet) { console.log(item); } // 2 // "some text" // 3 // 1Converting between Array and Set:
- You can create an Array from a Set using Array.from or the spread syntax.
- The Set constructor accepts an Array to convert in the other direction.
- Set objects store unique values—so any duplicate elements from an Array are deleted when converting!
Array.from(mySet); [ ...mySet2 ]; // convert to Array from Set mySet2 = new Set([1, 2, 3, 4]); // convert to Set from ArrayArray vs Set
- Deleting Array elements by value (arr.splice(arr.indexOf(val), 1)) is very slow.
- Set objects let you delete elements by their value.
- With an array, you would have to splice based on an element's index.
- The value
NaNcannot be found with indexOf in an array. - Set objects store unique values. You don't have to manually keep track of duplicates.
WeakSet object
- WeakSet objects are collections of garbage-collectable values, including objects and non-registered symbols.
- A value in the WeakSet may only occur once. It is unique in the WeakSet's collection.
- The WeakSet is weak: References to objects in the collection are held weakly.
- If there is no other reference to an object stored in the WeakSet, they can be garbage collected.
- WeakSets are not enumerable.
Key and value equality of Map and Set
- Both the key equality of Map objects and the value equality of Set objects are based on the SameValueZero algorithm:
- Equality works like the identity comparison operator ===.
- -0 and +0 are considered equal.
- NaN is considered equal to itself (contrary to ===).
11.Working with objects
- JavaScript is designed on an object-based paradigm. An object is a collection of properties,
- and a property is an association between a name (or key) and a value.
- A property's value can be a function, in which case the property is known as a method.
1.Creating new objects
using object initializer
- You can create an object using an
object initializer. - Object initializers are also called
object literals.
const obj = { property1: value1, // property name may be an identifier 2: value2, // or a number "property n" : value3, // or a string };- Objects created with initializers are called plain objects.
- You can create an object using an
Using a constructor function
- Define the object type by writing a
constructor function. - There is a strong convention, with good reason, to use a capital initial letter.
function Person(name, age, sex) { this.name = name; this.age = age; this.sex = sex; };- Now you can create an object called myCar as follows:
const rony = new Person("Rony McKinnon", 33, "M"); const ken = new Person("Ken Jones", 39, "M");- Car to include an owner property that takes a Person object
function Car(make, model, year, owner) { this.make = make; this.model = model; this.year = year; this.owner = owner; };- To instantiate the new objects, you then use the following:
const ronyCar = new Car("Eagle", "Talon TSi", 1993, rony); const kenCar = new Car("Nissan", "300ZX", 1992, ken);- find out the name of the owner of car2, you can access the following property:
kenCar.owner.name; // Rony McKinnon- add a property to a previously defined object. For example, the statement
ronyCar.color = "black"; kenCar.color = "red";- Define the object type by writing a
Using the Object.create() method
- Objects can also be created using the Object.create() method.
// Animal properties and method encapsulation const Animal = { type: "Invertebrates", // Default value of properties displayType() { // Method which will display type of Animal console.log(this.type); }, }; // Create new animal type called animal1 const animal1 = Object.create(Animal); animal1.displayType(); // Logs: Invertebrates // Create new animal type called fish const fish = Object.create(Animal); fish.type = "Fishes"; fish.displayType(); // Logs: Fishes
2.Objects and properties
Properties
- Object properties are basically the same as variables, except that they are associated with objects, not scopes.
- The properties of an object define the characteristics of the object.
- Like JavaScript variables, property names are case sensitive.
- Property names can only be strings or Symbols — all keys are converted to strings unless they are Symbols.
- Array indices are, in fact, properties with string keys that contain integers.
Accessing properties
// Dot notation myCar.make = "Ford"; myCar.model = "Mustang"; myCar.year = 1969; // Bracket notation myCar["make"] = "Ford"; myCar["model"] = "Mustang"; myCar["year"] = 1969; // Using variable Property let str = "myString"; myCar[str] = "This key is in variable str"; console.log(myObj.str); // undefined // The variable must be passed in bracket notation. console.log(myObj[str]); // 'This key is in variable str' console.log(myObj.myString); // 'This key is in variable str' // accessing any property as determined at runtime: let propertyName = "make"; myCar[propertyName] = "Ford"; // access different properties by changing the contents of the variable propertyName = "model"; myCar[propertyName] = "Mustang"; console.log(myCar); // { make: 'Ford', model: 'Mustang' } // Nonexistent properties of an object have value undefined (and not null). myCar.nonexistentProperty; // undefinedEnumerating properties
- There are three native ways to list/traverse object properties:
for...in:- This method traverses all of the enumerable string properties of an object as well as its prototype chain.
Object.keys(myObj):- This method returns an
arraywith only the enumerable own string property names - ("keys") in the object myObj but not those in the prototype chain.
- This method returns an
Object.getOwnPropertyNames(myObj)- This method returns an array containing all the own string property names in the object myObj
- regardless of if they are enumerable or not.
Deleting properties
- You can remove a
non-inheritedproperty using the delete operator.
const parent = { role: "Admin" }; const child = Object.create(parent); child.name = "Bob"; child.age = 15; console.log(parent); // { role: 'Admin' } console.log(child); // { name: 'Bob' , age:15 } console.log(child.role); // "Admin" console.log(child.name); // "Bob" console.log(child.age); // 15 delete child.age; // Does delete non-inherited properties delete child.role; // Does NOT delete inherited properties console.log(child.age); // undefined (deleted) console.log(child.role); // "Admin" (still accessible)- You can remove a
Inheritance
- All objects in JavaScript inherit from at least one other object.
- The object being inherited from is known as the
prototype, and the inherited properties can be found in the prototype object of the constructor. - Defining properties for all objects of one type:
- You can add a property to all objects created through a certain constructor using the prototype property.
- The following code adds a color property to all objects of type Car and then reads the property's value from an instance car1.
Car.prototype.color = "red"; console.log(car1.color); // "red"Defining methods
- A method is a function associated with an object.
- Or a method is a property of an object that is a function.
const myObj = { myMethod: function (params) { // do something }, // this works too! myOtherMethod(params) { // do something else }, }; // access the method like this: myObj.myMethod(arguments);- Asign new method later like this:
objectName.methodName = functionName;- and access the method like this:
objectName.methodName(params);Using
thisfor object references- this is a "hidden parameter" of a function call that's passed in by specifying the object before the function that was called.
const Manager = {name: "Karina",age: 27,job: "Software Engineer",}; const Intern = {name: "Tyrone",age: 21,job: "Software Engineer Intern",}; function sayHi() { console.log(`Hello, my name is ${this.name}`); } // add sayHi function to both objects Manager.sayHi = sayHi; Intern.sayHi = sayHi; Manager.sayHi(); // Hello, my name is Karina Intern.sayHi(); // Hello, my name is Tyrone- If you access the same function from another object, then
thiswill change as well. - If you use other methods to call the function, like
Function.prototype.call()orReflect.apply(), you can explicitly pass the value ofthisas an argument.
Defining getters and setters
- A
getteris a function associated with a property that gets the value of a specific property. - A
setteris a function associated with a property that sets the value of a specific property. Gettersandsetterscan be either defined withinobject initializers, or added later to any existing object.- Within object initializers, getters and setters are defined like regular methods, but prefixed with the keywords
getorset.
const myObj = { a: 7, get b() { return this.a + 1; }, c: this.a + 2, // will return NaN that is why we use get set d(x) { this.a = x / 2; this.b = x^2; this.c = x * 2; }, }; console.log(myObj.a); // 7 console.log(myObj.b); // 8, returned from the get b() method console.log(myObj.c); // NaN myObj.d = 50; // Calls the set c(x) method console.log(myObj.a); // 25 console.log(myObj.b); // value of b returned from the get b() method // even if value of b was modified in set d(x) // it still returned from the get b() method // and for now the value of a is 25, so it returns 26. console.log(myObj.c); // 100 console.log(myObj.d); // undefined // because the value itself is not being set here.myObj.a— a numbermyObj.b— a getter that returns myObj.a plus 1myObj.d— a setter that sets the value of myObj.a to half of the value myObj.c is being set toGettersandsetterscan also be added to an object at any time after creation using theObject.defineProperties()method.
{const myObj = { a: 0 }; Object.defineProperties(myObj, { b: {get() {return this.a + 1;},}, c: {set(x) {this.a = x / 2;},}, }); myObj.c = 10; // Runs the setter, which assigns (10 / 2) = 5 to the 'a' property console.log(myObj.b); // Runs the getter, which yields a + 1 or 6- A
Comparing objects
- Two distinct objects are never equal, even if they have the same properties.
// Two variables, two distinct objects with the same properties const flower = { name: "rose" }; const anotherFlower = { name: "rose" }; flower == anotherFlower; // return false flower === anotherFlower; // return false // Two variables, a single object const fruit = { name: "apple" }; const anotherFruit = fruit; // Assign fruit object reference to anotherFruit // Here fruit and anotherFruit are pointing to same object fruit == anotherFruit; // return true fruit === anotherFruit; // return true fruit.name = "grape"; // or // anotherFruit.name = "grape"; console.log(anotherFruit); // { name: "grape" }; not { name: "apple" }