var (function-scoped, avoid in modern code)let (block-scoped, recommended)const (block-scoped, constant)Example:
let name = "Alice";
const age = 25;
var city = "Berlin";
$, _Example:
let name = "Bob"; // String
let score = 95; // Number
let isActive = true; // Boolean
let data = null; // Null
let car; // Undefined
let user = { name: "Sam" }; // Object
let list = [1, 2, 3]; // Array
+, -, *, /, %, **
=, +=, -=, *=, /=
==, ===, !=, !==, <, >, <=, >=
&&, ||, !
++, --
if (score > 90) {
console.log("Great!");
} else if (score > 75) {
console.log("Good!");
} else {
console.log("Keep trying!");
}
let color = "green";
switch (color) {
case "red":
console.log("Stop");
break;
case "green":
console.log("Go");
break;
default:
console.log("Unknown color");
}
For Loop:
for (let i = 0; i < 5; i++) {
console.log(i);
}
While Loop:
let i = 0;
while (i < 5) {
console.log(i);
i++;
}
function greet(name) {
return `Hello, ${name}`;
}
const greet = (name) => `Hello, ${name}`;
const add = function(a, b) {
return a + b;
};
const person = {
name: "Jane",
age: 30,
greet: function() {
console.log("Hello!");
}
};
console.log(person.name);
person.greet();
const fruits = ["apple", "banana", "orange"];
console.log(fruits[0]); // "apple"
push()pop()shift()unshift()splice()slice()forEach()map()filter()