// var (pre-ES6)
var oldVariable = "old way";
// let (ES6+)
let modernVariable = "modern way";
// const (ES6+)
const CONSTANT_VALUE = "cannot be changed";
2.2.2 Tipe Data Primitif
// Number
let integer = 42;
let float = 3.14;
let infinity = Infinity;
let notANumber = NaN;
// String
let singleQuotes = 'Hello';
let doubleQuotes = "World";
let backticks = `Template literal`;
// Boolean
let isTrue = true;
let isFalse = false;
// Undefined
let undefinedVariable;
// Null
let nullValue = null;
// Symbol (ES6+)
let symbol = Symbol('description');
2.2.3 Type Coercion dan Conversion
Implicit coercion
Explicit conversion
Type checking dengan typeof
Common pitfalls dalam type coercion
2.3 Operator
2.3.1 Arithmetic Operators
let sum = 5 + 3; // Addition
let difference = 10 - 4; // Subtraction
let product = 6 * 2; // Multiplication
let quotient = 15 / 3; // Division
let remainder = 17 % 5; // Modulus
let power = 2 ** 3; // Exponentiation
// Increment & Decrement
let count = 0;
count++; // Postfix increment
++count; // Prefix increment
count--; // Postfix decrement
--count; // Prefix decrement
// AND, OR, NOT
let result1 = true && false; // false
let result2 = true || false; // true
let result3 = !true; // false
// Short-circuit evaluation
let a = null;
let b = a || "default"; // "default"
2.3.4 Assignment Operators
let x = 5;
x += 3; // x = x + 3
x -= 2; // x = x - 2
x *= 4; // x = x * 4
x /= 2; // x = x / 2
x %= 3; // x = x % 3
2.4 Struktur Kontrol
2.4.1 If Statement
let age = 18;
if (age >= 18) {
console.log("Anda sudah dewasa");
} else if (age >= 13) {
console.log("Anda remaja");
} else {
console.log("Anda masih anak-anak");
}
// Ternary operator
let status = age >= 18 ? "dewasa" : "belum dewasa";
2.4.2 Switch Statement
let day = "Monday";
switch (day) {
case "Monday":
console.log("Hari kerja");
break;
case "Saturday":
case "Sunday":
console.log("Akhir pekan");
break;
default:
console.log("Hari kerja biasa");
}
2.5 Loop
2.5.1 For Loop
// Standard for loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// For...of (untuk iterables)
let array = [1, 2, 3];
for (let item of array) {
console.log(item);
}
// For...in (untuk object properties)
let object = { a: 1, b: 2 };
for (let key in object) {
console.log(key, object[key]);
}
2.5.2 While dan Do-While Loop
// While loop
let count = 0;
while (count < 5) {
console.log(count);
count++;
}
// Do-while loop
let x = 0;
do {
console.log(x);
x++;
} while (x < 3);
2.6 Function
2.6.1 Function Declaration
// Basic function
function greet(name) {
return `Hello, ${name}!`;
}
// Function with default parameters
function multiply(a, b = 1) {
return a * b;
}
// Arrow function (ES6+)
const add = (a, b) => a + b;
// Function expression
const subtract = function(a, b) {
return a - b;
};
2.6.2 Function Parameters dan Return
// Rest parameters
function sum(...numbers) {
return numbers.reduce((total, num) => total + num, 0);
}
// Return multiple values
function getCoordinates() {
return {x: 10, y: 20};
}
2.7 Array
2.7.1 Array Operations
// Creating arrays
let fruits = ['apple', 'banana', 'orange'];
// Array methods
fruits.push('grape'); // Add to end
fruits.pop(); // Remove from end
fruits.unshift('mango'); // Add to beginning
fruits.shift(); // Remove from beginning
fruits.splice(1, 1); // Remove at position
// Array iteration
fruits.forEach(fruit => console.log(fruit));
let mappedFruits = fruits.map(fruit => fruit.toUpperCase());
let filteredFruits = fruits.filter(fruit => fruit.length > 5);