๐โจ JavaScript Notes ๐
๐งฑ Variables, Data Types & Operators ๐
๐ฆ Variables ๐๏ธ
Variable ๐ฆ โ a labeled box that stores a value that can be used later. ๐ท๏ธ
You can create it with:
-
let๐ -
const๐ -
var๐
var |
let |
const |
|
|---|---|---|---|
| Scope ๐ | Function-scoped โ | Block-scoped ๐งฑ | Block-scoped ๐งฑ |
| Reassignable โ๏ธ | โ | โ | โ |
| Redeclarable ๐ | โ | โ | โ |
let score = 10;
score = 20; // โ
reassignable
// Block-scoped ๐งฑ
const score = 20;
score = 10; // โ can't โ unreassignable ๐
// Block-scoped ๐งฑ
Block-scoped ๐งฑ โ let and const only exist inside the { } they were created in.
Function-scoped ๐ โ means var also exists throughout the whole function { }, ignoring inner block boundaries. ๐ช
function test() {
if (true) {
var leaked = "I escape the block!"; // ๐จ function-scoped
const trapped = "I stay inside the block!"; // ๐ block-scoped
let alsoTrapped = "I also stay inside the block!"; // ๐ block-scoped
// Inside the block: all three are visible
console.log(leaked); // "I escape the block!" โ
๐จ
console.log(trapped); // "I stay inside the block!" โ
๐
console.log(alsoTrapped); // "I also stay inside the block!" โ
๐
}
// Outside the block, still inside the function
console.log(leaked); // "I escape the block!" โ
๐จ
// Uncomment ONE line at a time to see the error:
// console.log(trapped); // ReferenceError: trapped is not defined โ๐
// console.log(alsoTrapped); // ReferenceError: alsoTrapped is not defined โ๐
}
test();
// Outside the function
// console.log(leaked); // ReferenceError: leaked is not defined โ
N/B ๐โ ๏ธ
-
letโ can't be redeclared ๐ซ๐- Can be reassigned โ๏ธโ
-
constโ can't be redeclared ๐ซ๐- Can't be reassigned ๐ซโ๏ธ
- Changing the *contents depends on the value: primitives are immutable ๐ง, objects/arrays are mutable ๐งฉ *
const person = {
name: "Brian", // ๐ค
age: 28 // ๐
};
person.age = 29; // โ
works โ mutating a property ๐ง
person.name = "John"; // โ
works too ๐ง
const person = { name: "Brian", age: 28 };
person = { name: "John", age: 29 }; // โ TypeError: Assignment to constant variable ๐
const fruits = ["apple", "grape"]; // ๐๐๐
fruits.push("orange");
console.log(fruits); // ["apple", "grape", "orange"] ๐งบโ
๐ฆ Data Type ๐๏ธ
Primitive/Immutable (Cannot be changed) ๐ง๐
- Strings ๐ค, Number ๐ข, Boolean โ โ, undefined, null, Symbol , BigInt
let name = "Brian"; // String ๐ค
let age = 22; // Number ๐ข (integers and decimals, e.g. 3.14)
let isStudent = true; // Boolean โ
โ (true or false)
let nothingYet; // undefined ๐คท (declared, but no value assigned)
let empty = null; // null ๐ซ (intentionally "no value")
let id = Symbol("id"); // Symbol ๐ (a unique identifier)
let big = 9007199254740993n; // BigInt ๐ (huge integers, note the n at the end)
Mutable ๐งฉ๐ โ objects, Arrays, functions
let name = "Brian"; // ๐ค
let age = 22; // ๐
let arr = ["apple", "grape", "banana"]; // ๐๐๐
functions -> function(){} -> function // โ๏ธ
- objects โ
{ }โ object ๐ฆ - arrays โ
[1,2,3]โ arrays, technically an object ๐ - functions โ
function(){}โ function โ๏ธ - Map ๐บ๏ธ
Special Quirks
-
console.log(typeof NaN)โnumberโ ๏ธ๐คฏ -
console.log(typeof null)โobjectโ ๏ธ๐ (an old bug that was never fixed, to avoid breaking existing code ๐ )
๐ฃ Hoisting ๐ช
Hoisting โ JavaScript registers declarations before running the code, so they behave as if lifted ๐ผ to the top of their scope. Only the declaration is lifted, not the value assigned to it. ๐
console.log(a); // undefined ๐คท
var a = 5;
var a โ hoisted โ
๐ผ
- Before assignment โ undefined ๐คท
-
5โ after the assignment line runs โ๏ธ
console.log(b); // ReferenceError ๐ฅ
let b = 5;
let/const...(Also hoisted ๐ผ but stay in TDZ โ Temporary Dead Zone โ)
๐งฎ Operators
1.### โ Arithmetic Operators
-
+โ add โ -
-โ subtract โ -
*โ multiply โ๏ธ -
/โ divide โ -
%โ remainder ๐ -
**โ exponent (power) โก
let power = 5 ** 2; // 25
let total = 7 % 3; // 1
2.### ๐ Assignment Operators
-
=โ assign ๐ฅ -
+=-=*=/=โ do the operation, then assign the result ๐ -
**=โ raise to a power, then assign โก
let x = 10;
x += 2; // 12
x **= 2; // 144
3.### โ๏ธ Comparison Operators
-
==โ loose equal (allows type conversion) ๐ค -
===โ strict equal (no type conversion) ๐ฏ -
!=โ loose not-equal -
!==โ strict not-equal -
>โ greater than -
<โ less than -
>=โ greater than or equal -
<=โ less than or equal
console.log(5 == "5"); // true (values match after conversion)
console.log(5 === "5"); // false (types differ) โ
prefer === in almost all cases
4.### ๐ Logical Operators
-
&&โ AND: both sides must be true ๐ค -
||โ OR: at least one side must be true ๐ -
!โ NOT: flips true/false ๐
console.log(true && false); // false
console.log(true || false); // true
console.log(!true); // false
5.### ๐ณ๏ธ Null Coalescing โโก๏ธ
(if the name/thing is null or undefined use...)
?? โ ๐
let name = null; // ๐ซ
let result = name ?? "Unknown";
console.log(result); // "Unknown" ๐ท๏ธ
?? โ use the right side only if the left is null or undefined
|| โ use the right side if the left is any falsy value (0, "", false, ...)
let count = 0;
console.log(count || 10); // 10 โ (0 is falsy, so || skips it)
console.log(count ?? 10); // 0 โ
(0 is not null/undefined, so ?? keeps it)
6.### ๐ Optional Chaining โ๏ธ
(Allows you to safely access a property that might not exist ๐ก๏ธ)
?. โ if the object/thing is null or undefined, stop and return undefined instead of crashing
let person = {};
console.log(person.address.city); // โ TypeError: Cannot read properties of undefined
console.log(person.address?.city); // undefined ๐คท
console.log(person?.address?.city); // undefined ๐คทโโ๏ธ (can chain as many `?.` as needed)
7.### โโ Increment / Decrement Operators ๐ผ๐ฝ
(increases/decreases a value by 1)
-
++โ increases value by 1 โฌ๏ธ -
--โ decreases value by 1 โฌ๏ธ
let count = 5;
count++;
console.log(count); // 6 ๐ข
8.### โ Ternary Operator ๐ญ
(short "if...then")
-
?โ used to test condition โ value ๐ง -
:โ if not โ then... โก๏ธ
let age = 90;
let result = age >= 60 ? "Grandfather" : "Not a Grandfather"; // ๐ด
console.log(result); // "Grandfather" ๐
9.### ๐ Type Operators ๐ต๏ธ
(work with types โ what a value's type is)
typeof โ examine primitive and non-primitive types ๐ฌ
instanceof โ checks if an object is an instance of a particular constructor/class(ONLY on Non-Primitive types) ๐๏ธ
let score = 10; // ๐ฏ
console.log(typeof score); // number ๐ข
Non-primitive types: typeof can't tell arrays apart โ๏ธ
let numbers = [];
console.log(typeof numbers); // "object" (arrays are technically objects) ๐
let numbers = [];
console.log(numbers instanceof Array); // true โ
10.### ๐ข Binary Operators ๐งฎโก
- Bit โ 0 or 1 ๐
- Computers ๐ป represent numbers using Binary
Decimal (10 digits): 0123456789 ๐
Binary represents numbers using powers of 2 โก:
- 2โฐ- 1
- 2ยน- 2
- 2ยฒ- 4
- 2ยณ- 8
-The bigger the decimal number, the more bits you need to represent it in binary.
-Start every bit position at 0 โ your blank template, e.g. for 4 bits: 0 0 0 0 (positions worth 8, 4, 2, 1).
- 2โฐ- 1- 0
- 2ยน- 2- 0
- 2ยฒ- 4- 0
- 2ยณ- 8- 0
"If we want to find which numbers make 5, we follow this steps โ" ๐งฎ-
**"5 is within 1 โ 8 (and we want 4 digits)"
This is saying: 5 fits somewhere in the range covered by 4 bits (which spans 0 up to 15, using the positions 8-4-2-1). So we set up our blank template with 4 zero-slots:
8 4 2 1
0 0 0 0
Step through each position, largest to smallest:
8 โ does 8 fit into 5? No โ bit stays 0
4 โ does 4 fit into 5? Yes โ bit becomes 1, remainder = 5 โ 4 = 1
2 โ does 2 fit into 1? No โ bit stays 0
1 โ does 1 fit into 1? Yes โ bit becomes 1, remainder = 1 โ 1 = 0
"5 = 4 + 1"
That's the record of which powers of 2 actually got used โ you only picked up a 1 at the 4-slot and the 1-slot, nothing else. Adding those back up (4 + 1 = 5) confirms you found the right combination.
"5 = 0101 โ
"
That's just reading the four slots left to right in the order you filled them:
8 โ 0
4 โ 1
2 โ 0
1 โ 1
โ 0101
6 = 4 + 2
- 8 โ 0
- 4 โ 1
- 2 โ 1
- 1 โ 0
6 = 0110 โ
๐ข Bitwise Operators โก
-
&โ AND โ -
|โ OR ๐ -
^โ XOR โก -
~โ NOT ๐ -
<<โ left shift โฌ ๏ธ -
>>โ right shift โก๏ธ -
>>>โ zero-fill right shift โก๏ธ0๏ธโฃ
& โ AND (both bits must be 1) ๐ค
Rule ๐
1 & 1 โ 1 โ
1 & 0 โ 0 โ
0 & 1 โ 0 โ
0 & 0 โ 0 โ
i.e 5 & 3
5 โ 0101
3 โ 0011
------
0001 โ came out 1 ๐ฏ
| โ OR (at least one must be 1) ๐
Rule ๐
1 | 1 โ 1 โ
1 | 0 โ 1 โ
0 | 1 โ 1 โ
0 | 0 โ 0 โ
i.e 4 | 6
4 โ 0100
6 โ 0110
------
0110 โ came out as 0110, same as 6 ๐ฏ
โก XOR ๐ฒ
(bits must be different)
Rule ๐
1 ^ 1 = 0 ๐
1 ^ 0 = 1 ๐
0 ^ 1 = 1 ๐
0 ^ 0 = 0 ๐
i.e 5 ^ 3
5 โ 0101
3 โ 0011
------
6 โ 0110 ๐ฏ
๐ซ NOT ๐
(bits are flipped to the opposite bit)
~5 โ -6 ๐ (JS flips all 32 bits; only the last 4 are shown as 1010)
We use the method ~n = -(n+1) โ easier than calculating it directly ๐ก๐ง
โฌ
๏ธ Left Shift << โช
(Shift every bit to the left and fill the empty spots on the right with 0. Each shift doubles the number.)
5 << 1 โ 1010 # 10 ๐
5 << 2 โ 10100 # 20 2๏ธโฃ0๏ธโฃ
5 << 3 โ 101000 # 40 4๏ธโฃ0๏ธโฃ
What it means:
3 << 1 โ 0011 โ 0110 #6 6๏ธโฃ
3 << 2 โ 0011 โ 01100 #12 ๐โ2๏ธโฃ
๐ฌ After the 1st left shift, the next shift you just add a zero and go on. โ0๏ธโฃ
โก๏ธ Right Shift >> โฉ
(You move every bit to the right โ you do a reverse of left shift ๐)
5 โ 0101
3 โ 0011
5 >> 1 โ 0010 #2 2๏ธโฃ
3 >> 1 โ 0001 #1 1๏ธโฃ
โก๏ธโก๏ธ >>> (also known as unsigned right shift) 0๏ธโฃ
- It always fills the left side with 0 โฌ ๏ธ0๏ธโฃ
- It's a bit more advanced for the basics we are covering here ๐
11.### ๐ Comma Operator ๐ข,๐ข
Only the last value is kept. ๐
let a = (1 + 2, 3 + 4);
console.log(a); // 7 ๐ฏ
12.### ๐ค String Operator โ๐
The + operator can also concatenate strings. ๐งท
let first = "Brian";
let last = "Kipchirchir";
let full = first + " " + last;
console.log(full); // "Brian Kipchirchir" ๐ค
Top comments (2)
One thing - the
%operator is the 'remainder' operator, not 'modulus'. JS does not have a built-in modulus operation. Remainder and modulus operations are similar but NOT the same.developer.mozilla.org/en-US/docs/W...
Thank you sir for the correction.I appreciate that.