DEV Community

Brian Kipchirchir
Brian Kipchirchir

Posted on Edited on

Day 1: Variables, Data Types & Operators

๐Ÿ““โœจ 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 ๐Ÿงฑ
Enter fullscreen mode Exit fullscreen mode
const score = 20;
score = 10;       // โŒ can't โ€” unreassignable ๐Ÿ”’
// Block-scoped ๐Ÿงฑ
Enter fullscreen mode Exit fullscreen mode

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 โŒ
Enter fullscreen mode Exit fullscreen mode

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 ๐Ÿ”ง
Enter fullscreen mode Exit fullscreen mode
const person = { name: "Brian", age: 28 };
person = { name: "John", age: 29 }; // โŒ TypeError: Assignment to constant variable ๐Ÿ”’
Enter fullscreen mode Exit fullscreen mode
const fruits = ["apple", "grape"];  // ๐ŸŽ๐Ÿ‡๐ŸŠ
fruits.push("orange");
console.log(fruits); // ["apple", "grape", "orange"] ๐Ÿงบโœ…
Enter fullscreen mode Exit fullscreen mode

๐Ÿ“ฆ 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)
Enter fullscreen mode Exit fullscreen mode

Mutable ๐Ÿงฉ๐Ÿ”“ โ€” objects, Arrays, functions

let name = "Brian";        // ๐Ÿ‘ค
let age = 22;              // ๐ŸŽ‚
let arr = ["apple", "grape", "banana"];   // ๐ŸŽ๐Ÿ‡๐ŸŒ
functions -> function(){} -> function     // โš™๏ธ
Enter fullscreen mode Exit fullscreen mode
  • 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;
Enter fullscreen mode Exit fullscreen mode

var a โ†’ hoisted โœ…๐Ÿ”ผ

  • Before assignment โ†’ undefined ๐Ÿคท
  • 5 โ†’ after the assignment line runs โœ๏ธ
console.log(b);   // ReferenceError ๐Ÿ’ฅ
let b = 5;
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

5.### ๐Ÿ•ณ๏ธ Null Coalescing โ“โžก๏ธ
(if the name/thing is null or undefined use...)

?? โ†’ ๐Ÿ”„

let name = null;   // ๐Ÿšซ
let result = name ?? "Unknown";
console.log(result); // "Unknown" ๐Ÿท๏ธ
Enter fullscreen mode Exit fullscreen mode

?? โ†’ 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)
Enter fullscreen mode Exit fullscreen mode

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)
Enter fullscreen mode Exit fullscreen mode

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 ๐Ÿ”ข
Enter fullscreen mode Exit fullscreen mode

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" ๐ŸŽ‰
Enter fullscreen mode Exit fullscreen mode

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 ๐Ÿ”ข
Enter fullscreen mode Exit fullscreen mode

Non-primitive types: typeof can't tell arrays apart โš›๏ธ

let numbers = [];
console.log(typeof numbers); // "object" (arrays are technically objects) ๐Ÿ“‹
Enter fullscreen mode Exit fullscreen mode
let numbers = [];
console.log(numbers instanceof Array); // true โœ…
Enter fullscreen mode Exit fullscreen mode

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  โŒ
Enter fullscreen mode Exit fullscreen mode

i.e 5 & 3

5 โ†’ 0101
3 โ†’ 0011
------
0001 โ†’ came out 1 ๐ŸŽฏ
Enter fullscreen mode Exit fullscreen mode

| โ†’ OR (at least one must be 1) ๐Ÿ”€

Rule ๐Ÿ“

1 | 1 โ†’ 1  โœ…
1 | 0 โ†’ 1  โœ…
0 | 1 โ†’ 1  โœ…
0 | 0 โ†’ 0  โŒ
Enter fullscreen mode Exit fullscreen mode

i.e 4 | 6

4 โ†’ 0100
6 โ†’ 0110
------
0110 โ†’ came out as 0110, same as 6 ๐ŸŽฏ
Enter fullscreen mode Exit fullscreen mode

โšก XOR ๐ŸŽฒ

(bits must be different)

Rule ๐Ÿ“

1 ^ 1 = 0  ๐Ÿ”
1 ^ 0 = 1  ๐Ÿ”€
0 ^ 1 = 1  ๐Ÿ”€
0 ^ 0 = 0  ๐Ÿ”
Enter fullscreen mode Exit fullscreen mode

i.e 5 ^ 3

5 โ†’ 0101
3 โ†’ 0011
------
6 โ†’ 0110  ๐ŸŽฏ
Enter fullscreen mode Exit fullscreen mode

๐Ÿšซ NOT ๐Ÿ”„

(bits are flipped to the opposite bit)

~5 โ†’ -6  ๐Ÿ”ƒ  (JS flips all 32 bits; only the last 4 are shown as 1010)
Enter fullscreen mode Exit fullscreen mode

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๏ธโƒฃ
Enter fullscreen mode Exit fullscreen mode

What it means:

3 << 1 โ†’ 0011 โ†’ 0110 #6   6๏ธโƒฃ
3 << 2 โ†’ 0011 โ†’ 01100 #12 ๐Ÿ”Ÿโž•2๏ธโƒฃ
Enter fullscreen mode Exit fullscreen mode

๐Ÿ’ฌ 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๏ธโƒฃ
Enter fullscreen mode Exit fullscreen mode

โžก๏ธโžก๏ธ >>> (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 ๐ŸŽฏ
Enter fullscreen mode Exit fullscreen mode

12.### ๐Ÿ”ค String Operator โž•๐Ÿ“
The + operator can also concatenate strings. ๐Ÿงท

let first = "Brian";
let last = "Kipchirchir";
let full = first + " " + last;
console.log(full); // "Brian Kipchirchir" ๐Ÿ‘ค
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
jonrandy profile image
Jon Randy ๐ŸŽ–๏ธ • Edited

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...

Collapse
 
briankipchirchir77 profile image
Brian Kipchirchir

Thank you sir for the correction.I appreciate that.