DEV Community

Cover image for Loops and Function
Kamalesh AR
Kamalesh AR

Posted on

Loops and Function

For Loop

Next is the for loop. It should be the go to way to do something a certain number of times. If you need to repeat an operation, say, 10 times, then use a for loop instead. This particular loop may be intimidating to those new to programming, but rewriting the same loop in the while-style loop can help illustrate the syntax make it easier to stick in your mind.

Syntax

for (initialization; condition; incrementation) {
        // Execute the loop body as long as the condition is true
    statement(s);
}
Enter fullscreen mode Exit fullscreen mode
  • Statement 1: It is the initialization of the counter. It is executed once before the execution of the code block.
  • Statement 2: It defines the testing condition for executing the code block
  • Statement 3: It is the increment or decrement of the counter & executed (every time) after the code block has been executed.

JavaScript for loop is a control flow statement that allows code to be executed repeatedly based on a condition. It consists of three parts: initialization, condition, and increment/decrement.

Example:

let x = 5
for (let i = 1; i <= 10; i++) {
  console.log(x * i); 
}
Enter fullscreen mode Exit fullscreen mode

Output:

5
10
15
20
25
30
35
40
45
50

Do while loop

A do...while loop in JavaScript is a control structure where the code executes repeatedly based on a given boolean condition. It's similar to a repeating if statement. One key difference is that a do...while loop guarantees that the code block will execute at least once, regardless of whether the condition is met initially or not.

There are mainly two types of loops.

  • Entry Controlled loops: In this type of loop, the test condition is tested before entering the loop body. For Loop and While Loops are entry-controlled loops.
  • Exit Controlled Loops: In this type of loop the test condition is tested or evaluated at the end of the loop body. Therefore, the loop body will execute at least once, irrespective of whether the test condition is true or false. the do-while loop is exit controlled loop.

Syntax

do {
    // Statements
}
while(conditions)
Enter fullscreen mode Exit fullscreen mode

Example:

let test = 1;
do {
    console.log(test);
    test++;
} while(test<=5)
Enter fullscreen mode Exit fullscreen mode

Output:

1
2
3
4
5

Functions:

Functions in JavaScript are reusable blocks of code designed to perform specific tasks. They allow you to organize, reuse, and modularize code. It can take inputs, perform actions, and return outputs.

Functions are one of the fundamental building blocks in JavaScript. A function in JavaScript is similar to a procedure—a set of statements that performs a task or calculates a value, but for a procedure to qualify as a function, it should take some input and return an output where there is some obvious relationship between the input and the output. To use a function, you must define it somewhere in the scope from which you wish to call it.

Understanding Functions

In functions, parameters are placeholders defined in the function, while arguments are the actual values you pass when calling the function.

Example:

function greet(name) {   // 'name' is a parameter
  console.log("Hello " + name);
}

greet("Alice");  // "Alice" is the argument
Enter fullscreen mode Exit fullscreen mode

Output:

Hello ALice

Default Parameters

  • Default parameters are used when no argument is provided during the function call.
  • If no value is passed, the function automatically uses the default value.

Example:

function greet(name = "Guest") {
  console.log("Hello, " + name);
}

greet();
greet("Aman");
Enter fullscreen mode Exit fullscreen mode

Ouput:

Hello, Guest

Hello, Aman

Return Statement

  • The return statement is used to send a result back from a function.
  • When return executes, the function stops running at that point.
  • The returned value can be stored in a variable or used directly.

Example:

function add(a, b) {
  return a + b; // returns the sum
}

let result = add(5, 10);
console.log(result);
Enter fullscreen mode Exit fullscreen mode

Output:

15

References:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Functions
https://www.geeksforgeeks.org/javascript/functions-in-javascript/
https://www.geeksforgeeks.org/java/java-do-while-loop-with-examples/
https://css-tricks.com/all-about-javascript-loops/
https://mimo.org/glossary/javascript/for-loops

Top comments (0)