- JS variable
const PI = 3.14159;
- JS loop
for (let i = 0; i < 5; i++) {
console.log("Index: " + i);
}
- JS conditional statement
let x = 10;
if (x > 5) {
console.log("x is greater than 5");
} else {
console.log("x is less than or equal to 5");
}
- JS object
let person = {
name: "John",
age: 30,
city: "New York"
};
- JS array
let numbers = [1, 2, 3, 4, 5];
- JS function
function greet(name) {
console.log("Hello, " + name + "!");
}
- JS array method (push)
let fruits = ["apple", "banana"];
fruits.push("orange");
- JS array method (pop)
let fruits = ["apple", "banana", "orange"];
fruits.pop();
- JS array method (forEach)
let numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number) {
console.log(number);
});
- JS string method (concat)
let str1 = "Hello, ";
let str2 = "world!";
let greeting = str1.concat(str2);
- JS string method (toUpperCase)
let str = "hello";
let upperCaseStr = str.toUpperCase();
- JS string method (slice)
let str = "Hello, world!";
let slicedStr = str.slice(7);
- JS string method (indexOf)
let str = "Hello, world!";
let index = str.indexOf("world");
- JS string method (replace)
let str = "Hello, world!";
let newStr = str.replace("world", "mars");
- JS string method (split)
let str = "apple, banana, orange";
let fruits = str.split(", ");
- JS object method (Object.keys)
let person = {
name: "John",
age: 30,
city: "New York"
};
let keys = Object.keys(person);
- JS object method (Object.values)
let person = {
name: "John",
age: 30,
city: "New York"
};
let values = Object.values(person);
- JS object method (Object.entries)
let person = {
name: "John",
age: 30,
city: "New York"
};
let entries = Object.entries(person);
- JS Math object (Math.PI)
let pi = Math.PI;
- JS Math object (Math.round)
let rounded = Math.round(4.7);
- JS Math object (Math.floor)
let roundedDown = Math.floor(4.7);
- JS Math object (Math.ceil)
let roundedUp = Math.ceil(4.3);
- JS Math object (Math.random)
let randomNumber = Math.random();
- JS Date object (new Date)
let currentDate = new Date();
- JS Date object (getMonth)
let month = currentDate.getMonth();
- JS Date object (getFullYear)
let year = currentDate.getFullYear();
- JS Date object (getDay)
let day = currentDate.getDay();
- JS Date object (getDate)
let date = currentDate.getDate();
- JS Date object (getHours)
let hours = currentDate.getHours();
- JS Date object (getMinutes)
let minutes = currentDate.getMinutes();
- JS Date object (getSeconds)
let seconds = currentDate.getSeconds();
- JS Date object (getMilliseconds)
let milliseconds = currentDate.getMilliseconds();
- JS Date object (toDateString)
let dateString = currentDate.toDateString();
- JS Date object (toISOString)
let isoString = currentDate.toISOString();
- JS Date object (toLocaleDateString)
let localeDateString = currentDate.toLocaleDateString();
- JS Date object (toLocaleTimeString)
let localeTimeString = currentDate.toLocaleTimeString();
- JS Date object (toLocaleString)
let localeString = currentDate.toLocaleString();
- JS Date object (UTC methods)
let utcDate = currentDate.toUTCString();
- JS array method (forEach with index)
let numbers = [1, 2, 3, 4, 5];
numbers.forEach(function(number, index) {
console.log("Index: " + index + ", Value: " + number);
});
- JS array method (map)
let numbers = [1, 2, 3, 4, 5];
let doubled = numbers.map(function(number) {
return number * 2;
});
- JS array method (filter)
let numbers = [1, 2, 3, 4, 5];
let evenNumbers = numbers.filter(function(number) {
return number % 2 === 0;
});
- JS array method (reduce)
let numbers = [1, 2, 3, 4, 5];
let sum = numbers.reduce(function(accumulator, currentValue) {
return accumulator + currentValue;
}, 0);
- JS array method (every)
let numbers = [2, 4, 6, 8, 10];
let allEven = numbers.every(function(number) {
return number % 2 === 0;
});
- JS array method (some)
let numbers = [1, 2, 3, 4, 5];
let hasEven = numbers.some(function(number) {
return number % 2 === 0;
});