The basic meaning of a loop in programming is:
A loop is a way to repeat a set of instructions multiple times, automatically.
Repeat tasks easily
Handle repetitive work with less code
for loop, while loop, do-while-loop for of, for in loop
When you know how much time the code execute then for off loop used. For off loop easy to use example shown below:
for (let i = 0; i < 5; i++) {
console.log("Count: " + i);
}
The loop excutes block of codes untill condition true.
let i = 0
while (i > 0 ) {
i++
console.log(i) }
In this type of loop code execute one time untill the condition false.
let i = 0
do {
i++
console.log(i)
} while (i > 10);
This type of loop in iterable values like in arrays
const fruits = ["apple", "banana",
"cherry"];
for (let fruit of fruits) {
console.log(fruit);
}
This type of loop used in objects
const person = { name: "John", age: 30 };
for (let key in person) {
console.log(key + ": " + person[key]);
}
A function is block or code that runs when it is called.
Functions helps us reuse code instead of writing it again and again, whenever we want to
use we just call it.
function functionName(parameters) {
// code to execute
}
There are two main types of functions in any other programming language these are built-in-function and user-defined-functions.
function functionName(parameters) {
// code to execute
}
The built-in-function are the standards that was defined by language and these have the specific functions defined already and cannot use for own purposes. The user-defined-functions are built by user and could be used for personal purposes.
function functionName(parameters) {
// code to execute
}
Some built-in functions which have specific meanings in js are as follows:
parseInt(), parseFloat(), isNaN(), isFinite()
push(), toUpperCase(), random()
Some built-in functions which have specific meanings in js are as follows:
parseInt(), parseFloat(), isNaN(), isFinite()
push(), toUpperCase(), random()
A named function has a specific name that you can use to call it later.
function greet() {
console.log("Hello, World!");
}
greet(); // Output: Hello, World!
A named function has a specific name that you can use to call it later.
function greet() {
console.log("Hello, World!");
}
greet(); // Output: Hello, World!
An arrow function is a shorter way to write anonymous functions.
const greet = () => {
console.log("Hello from arrow function!");
};
greet(); // Output: Hello from arrow function!