Photo by Markus Spiske on Unsplash

HOW ABOUT JavaScript?

Let’s start with Conditionals and Loops.

Rashini Gamalath
Published in
6 min readFeb 18, 2021

--

Credit goes to EscuelaDevRock

Hello everyone!

This is my very first article in the medium. Today I am going to talk about the very basic concepts of JavaScript.

We also want to perform different actions based on different circumstances when we write a code. And this is where we talk about the conditional statements.

The if Statement

A bunch of different conditionals is available in JavaScript, as a starting point I would like to talk about one of the most useful conditions: “if

We use the if condition to define a code block that we want to execute if it is valid for a specified condition.

if (condition) {    statement}

The statements which are inside the block will only be executed if the condition is true.

Here is a small example:

The output of the code:

But think, if the above condition is false, then the alert statement gets skipped and the program will continue the line after the closing if statement {}.

The else & else-if statement

We can use the else statement to specify a block of code that the same condition is false.

if (expression) {// executed if condition is true}else  {// executed if condition is false}

And we use the else-if condition to specify a new condition to test if the first condition is false.

if (expression) {// executed if condition is true}else if (expression) {// executed if condition is false}else {// executed if the all above conditions are false}

Here is a small example:

The output of the code:

The switch statement

Just think, if you need to choose multiple conditions? that's where the switch statements coming for. The switch expression is evaluated once. The value of the expression is compared with the values of each case. If there is a match, the associated block of code is executed. If there is no match, the default code block is executed.

switch (expression) {     case n1:        statements        break;     case n2:        statements        break;     default:        statements}

Note that a break should be put in each case statement, otherwise the program moves to the next labeled statement without break, executing the statements until a break or the end of the statement is reached. In certain cases, this consistency can be desirable.

Here is a small example:

The output of the code:

The for Loop

If you want to execute the same code over and over again with different values, you can use the for Loop.

The for loop has three parts: Initialization part, Test condition, and Iteration statement. The initialization of the loop in which our counter is initialized to a starting value. Before the loop starts, the initialization statement is executed. The test statement will test whether or not a given condition is true. If the condition is true, it will execute the code given inside the loop, otherwise, the control will come out of the loop. The iteration statement in which your counter can be increased or decreased.

for (initialization; test condition; iteration statement) {  Statement(s) to be executed if test condition is true }

Here is a small example:

The output of the code:

🤔What’s actually happening here?

In the above example, statement 1 (var i = 1;) sets a variable before the loop starts, then statement 2 (i≤5;) defines the condition for the loop to run(in here (i) should be less than or equal to 5). In statement 3 increases, the (i) value each time by 1 the code block in the loop has been executed.

The for/in Loop

We use for/in the loop to iterate over the properties of an object. If we want to read the properties of an object, we can use it for/in Loop.

Object?🤔

Object whose non-symbol enumerable properties are iterated over.

for (variable in object) {  
Statement
}

Every time that variable name is changed as per the object property.

Variable?🤔

A different property name is assigned to the variable on each iteration.

Here is a small example:

The output of the code:

🤔What’s happening here?

In for/in Loop, every time it takes the first name. Then the value of a person is substituted by the lname, then by the age. Like that in each iteration the value of the person changes. Then it appending the read properties in the people, value of person in the people object to this txt variable.

The While Loop

Why do we use while loops?

A while loop tends to continuously execute a code block as long as a condition is true. The loop terminates until the expression becomes false.

while (expression) {  
Statement(s) to be executed if expression is true.
}

THERE IS A SPECIAL NOTE: You have to think about the expression when you are writing the code. If your condition is always true, the loop will run infinitely. Endless loops are very bad. And if we fail to increase the variable used in the condition, one way of this happening is. So you have to be careful!!

Here is a small example:

The output of the code:

The while loop in the above code will continue to run as long as the count variable is less than, 5. And each time the loop runs, it will increase by 1.

The Do…While Loop

Nearly done with loops! This is the last one!!!

The do-while loop is similar to the while loop, except that at the end of the loop, the condition check occurs. This ensures that at least once, even though the condition is incorrect, the loop will still be executed.

do {  
Statement(s) to be executed.
} while (expression);

Here is a small example:

The output of the code:

That’s all about Conditionals and Loops. So I think you got some basic knowledge about what are the loops and how those loops are working.

So If you think this is important, please give me a clap, leave a comment & share it with the others.

--

--

Rashini Gamalath
CodeX
Writer for

Undergraduate of University of Moratuwa, Faculty of Information Technology.