Syntax
Hello World
console.log("Hello World");
Rules
- Case-sensitive
Naming Conventions
- camelCase everywhere
- UPPERCASE for constants
- PascalCase for classes
Comments
/*Multiple Line*/
//Single Line
Variables
Declaration
Initialization
Both at once
Constants
Data types
Primitives
bigInt
- Used to store values where max limit might exceed the integer range.
//Two ways of creating bigInt
let x = 123n;
let x = BigInt(123);
Compound
Array
Declaration
Initialization
Strings
Declaration
Initialization
Both at once
Length
Looping/character at index
Structures
Interfaces
Classes
class User{
constructor(email, name){
this.email = email;
this.name = name;
}
login(){
//implementation
}
}
Creating objects
All ways use Object.create()
internally.
Object Literal Notation: Independent objects
let chinsingh = {
email: 'chinsingh@outlook.in',
name: 'Chinmay Singh',
login(){
//implementation
}
};
Using classes
let cs = new User('chinsingh@outlook.in','Chinmay Singh');
Defining just constructor
//constructor
function Bike(gears, startGear) {
this.gears = gears;
this.currentGear = startGear;
}
//function as part of Bike's prototype
Bike.prototype.changeGear = function(direction,changeBy){
//logic to change gear
}
const bike = new Bike(10, 3);
console.log(bike.gears); // 10
console.log(bike.currentGear); //3
bike.changeGear('up', 1);
console.log(bike.currentGear); //4
Using objects
//using dot operator
chinsingh.email = 'chinmaysingh@outlook.in'
// using bracket notation -> property names can be dynamic
console.log(chinsingh['name']);
//adding new properties to objects
chinsingh.age = 30;
Operators
Spread/rest operator
...
:-
Context within which it is used determines whether it is a spread or rest operator.
-
Spread: Used to expand iterables (like array and objects) into individual elements
- Array Expansion
const arr1 = [1, 2, 3]; const arr2 = [...arr1, 4, 5]; console.log(arr2); // Output: [1, 2, 3, 4, 5]
- Function Arguments
const numbers = [1, 2, 3]; const sum = (a, b, c) => a + b + c; console.log(sum(...numbers)); // Output: 6
- Object Spread
const obj1 = { a: 1, b: 2 }; const obj2 = { ...obj1, c: 3 }; console.log(obj2); // Output: { a: 1, b: 2, c: 3 }
- Array Expansion
-
Rest: Used to collect all arguments into an array.
function sum(...args) { return args.reduce((acc, val) => acc + val, 0); } console.log(sum(1, 2, 3, 4)); // Output: 10
-
Comparison
==
/ !=
- Type is not taken into account for comparing.
- Implicit Type Coercion is performed to compare both sides.
===
/ !==
- Type is taken into account as well for comparison.
- Primitive types are only equivalent when both type and value match
- Object comparisons are only true when their respective pointers are pointing to the same memory address
Flow
Conditionals
Loops
Functions/Methods
Modifiers
static
: belongs to class
Gives error if used with object.