JavaScript loops and string concatenation:
for Loopfor loop is used when the number of iterations is known beforehand.for (initialization; condition; increment/decrement) {
// code to be executed
}
for (let i = 0; i < 5; i++) {
console.log("Iteration: " + i);
}
for/in Loopfor/in loop is used to iterate over the properties (keys) of an object.for (key in object) {
// code to be executed
}
const person = {name: "Alice", age: 25, city: "New York"};
for (let key in person) {
console.log(key + ": " + person[key]);
}
person object and prints the key-value pairs.for/of Loopfor/of loop is used to iterate over iterable objects like arrays, strings, or other collection types.for (variable of iterable) {
// code to be executed
}
const fruits = ["apple", "banana", "cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
fruits array and prints the element.while Loopwhile loop is used when the number of iterations is not known, and the loop continues as long as the condition is true.while (condition) {
// code to be executed
}
let i = 0;
while (i < 5) {
console.log("Iteration: " + i);
i++;
}
do/while Loopdo/while loop is similar to the while loop, but it guarantees that the code block is executed at least once.do {
// code to be executed
} while (condition);
let i = 0;
do {
console.log("Iteration: " + i);
i++;
} while (i < 5);
console.log:
+ operator.let name = "Alice";
console.log("Hello, " + name + "!");
${} inside backticks (`).let name = "Alice";
console.log(`Hello, ${name}!`);