Conditional statements in Js
Conditional statements are used to perform different actions based on different conditions.
Conditional Statements
Very often when you write code, you want to perform different actions for different decisions.
You can use conditional statements in your code to do this.
In JavaScript we have the following conditional statements:
- Use
if
to specify a block of code to be executed, if a specified condition is true - Use
else
to specify a block of code to be executed, if the same condition is false - Use
else if
to specify a new condition to test, if the first condition is false - Use
switch
to specify many alternative blocks of code to be executed
Example
if - else Statement
Use the else
statement to specify a block of code to be executed if the condition is false.
Ternary operators
Ternary Operators , are also known as conditional operators. Basically, they put an if else statement onto one line.
- if statements: if a condition is true, it executes a specific block of code.
- else statements: if the same condition is false, it executes a specific block of code. else if: this specifies a new test if the first condition is false.
var hour = 14;
if(hour >= 6 && hour < 12){ console.log("Good Morning"); }else if (hour >= 12 && hour < 18){ console.log("Good Agternoon"); }else{ console.log("Good Evening") }
Switch Statement
var hour = 14;
The switch
statement is used to perform different actions based on different conditions.
This is how it works:
- 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.
Example :
Loops in JavaScript
Loops are something that is with us always since the beginning of our never ending journey of learning JavaScript(or any programming language) so let's focus on them.
What loops ?
Loop is divided into 3Parts
1) Initialization
2) condation ,
( condition is true you will get result OR condition is false,it will go out of loop )
3) increment / decrement
loops are ?
*while loop
*do...While loop
*for
*for..in
*for...of
So first let’s define what is a loop and why we need to use them in JavaScript?
Do you know when you like a music so much that you just listening it over and over? Well that is a loop. For programming a loop is a way to iterate over something like an array or an object for example. With loops we make sure our code is DRY. The loop will loop till the condition is met or loop if the condition is met and till the condition is false. A infinite loop is loop that has not ending and most likely it will crash your app/website
While Loop
The while loop it runs as long the condition evaluated is true. Think of it as a conditional, an if statement but instead of running once, run as long the condition is true.
If you don’t write the condition or write a condition that will never turn false
do...while Loop
Now let's see a situation where it only run once:
for loop
One of the most used loops in JavaScript is the for loop. When I start coding with JS, this was one of the ones I used more till today. In the beginning I was not getting so much how it work so I try to explain in a way that I would understand when I started
In the for loop, the code is repeated until the condition evaluates to false.
One of the uses of the for loop is to loop over an array.
Syntax:
So when in use, would be looking something like this: