Displays "Hello World" in the console.
console.log("Hello World");
Declare variables and print them.
let x = 10;
let y = 5;
console.log(x + y);
Simple if-else example in JavaScript.
let age = 18;
if(age >= 18){
console.log("Adult");
}else{
console.log("Minor");
}
Loop through numbers 1 to 5.
for(let i = 1; i <= 5; i++){
console.log(i);
}
Execute while a condition is true.
let count = 0;
while(count < 5){
console.log(count);
count++;
}
Define and call a function.
function greet(name){
console.log("Hello " + name);
}
greet("Alice");
Create an array and loop through it.
let fruits = ["apple","banana","cherry"];
fruits.forEach(fruit => console.log(fruit));
Create a simple object and access properties.
let person = {name:"Alice", age:25};
console.log(person.name);
Generate a random number between 1 and 10.
let rand = Math.floor(Math.random()*10)+1;
console.log(rand);
Change content of an HTML element.
document.getElementById("demo").innerText = "Hello!";