Variables
JavaScript provides three ways to declare variables: var, let, and const. let and const are block‑scoped, while var is function‑scoped.
let name = "Alice";
const PI = 3.14159;
var count = 0;
Functions
Functions are reusable blocks of code. They can be declared in several ways:
// Function Declaration
function greet(person) {
return `Hello, ${person}!`;
}
// Function Expression
const square = function(x) {
return x * x;
};
// Arrow Function
const add = (a, b) => a + b;
Loops
Loops let you repeat actions. JavaScript supports for, while, and do…while loops.
for (let i = 0; i < 5; i++) {
console.log(i);
}
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
Try It Yourself
Write JavaScript code in the editor below and see the output instantly.