1)If…Else
The if statement executes a statement if a specified condition is true. If the condition is false, another statement can be executed.
Example:
var tomorrow = 'rain';
if(tomorrow == 'rain'){
console.log('take a raincoat');
}else{
console.log('No need to take a raincoat');
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
take a raincoat
PS C:UsersThe-BeastDesktopjavascript Blog>
Example:
var tomorrow = 'no rain';
if(tomorrow == 'rain'){
console.log('take a raincoat');
}else{
console.log('No need to take a raincoat');
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
No need to take a raincoat
PS C:UsersThe-BeastDesktopjavascript Blog>
2) switch Statement
Evaluates an expression, matching the expression’s value to a case clause, and executes statements associated with that case.
1st without a break statement.
Example: Find the Area of circle, triangle, and rectangle?
var area = "circle" ;
var PI = 3.142, l=6, b=5, r=4;
if(area == "circle"){
console.log("the area of the circle is : " + PI*r**2);
}else if(area == "triangle"){
console.log("the area of the triangle is : " + (l*b)/2);
}else if(area == "rectangle"){
console.log("the area of the rectangle is : " + (l*b));
}else{
console.log("please enter valid data");
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
the area of the circle is : 50.272
PS C:UsersThe-BeastDesktopjavascript Blog>
using break statement
Example:
var area = "circle" ;
var PI = 3.142, l=6, b=5, r=4;
switch(area){
case 'circle':
console.log("the area of the circle is : " + PI*r**2);
break;
case 'triangle':
console.log("the area of the triangle is : " + (l*b)/2);
break;
case 'rectangle':
console.log("the area of the rectangle is : " + (l*b));
break;
default:
console.log("please enter valid data");
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
the area of the circle is : 50.272
PS C:UsersThe-BeastDesktopjavascript Blog>
break
Terminates the current loop, switch, or label statement and transfers program control to the statement following the terminated statement.
continue
Terminates execution of the statements in the current iteration of the current or labeled loop, and continues execution of the loop with the next iteration.
3)While Loop Statement
The while statement creates a loop that executes a specified statement as long as the test condition evaluates to true.
Example:
var num=0;
while(num <= 10){
console.log(num);
num++;
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
0
1
2
3
4
5
6
7
8
9
10
PS C:UsersThe-BeastDesktopjavascript Blog>
4)Do-While Loop Statement
The do…while loop is similar to the while loop except that the condition check happens at the end of the loop. This means that the loop will always be executed at least once, even if the condition is false.
Example:
var num = 20;
do{
console.log(num);
num++;
}while(num <= 10);
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
20
PS C:UsersThe-BeastDesktopjavascript Blog>
5)For Loop
Looping in programming languages is a feature that facilitates the execution of a set of instructions/functions repeatedly while some condition evaluates to true. A Loop executes the sequence of statements many times until the stated condition becomes false.
Example:
for(var num = 0; num <= 10; num++){
console.log(num);
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
0
1
2
3
4
5
6
7
8
9
10
PS C:UsersThe-BeastDesktopjavascript Blog>
What are truthy and falsy values in Javascript?
we have a total of 5 falsy values in javascript.
0,” “,undefined,null,NaN,false.
Example:
if(score = 0){
console.log("Yay, We won the game ");
}else{
console.log("OMG, we loss the game ");
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
OMG, we loss the game
PS C:UsersThe-BeastDesktopjavascript Blog>
Example:
if(score = 50){
console.log("Yay, We won the game ");
}else{
console.log("OMG, we loss the game ");
}
output:
PS C:UsersThe-BeastDesktopjavascript Blog> node index.js
Yay, We won the game
PS C:UsersThe-BeastDesktopjavascript Blog>
watch the video:https://youtu.be/KGkiIBTq0y0
Hello, I want to subscribe for this weblog to take newest updates, so where can i do it please help out.
You are so cool! I do not suppose I have read something like this before.
So great to discover somebody with some unique thoughts on this subject.
Really.. many thanks for starting this up.
This website is one thing that is required on the web, someone with a bit of originality!
Very nice article, totally what I was looking for.
Οh my g᧐odness! Amazing article dude! Many thanks, However
I am experiencing troublеs with your RSS. I don’t know the reason why I am unable to join it.
Іs tere anyonne еlse haѵing identical RSS issues? Anyone thɑt
knows the answer can you kindly rеspߋnd? Thanx!!
Thank you, I have just been looking for info approximately this
topic for a long time and yours is the best I have came
upon so far. But, what concerning the conclusion? Are you
sure in regards to the source?
What’s up, I want to subscribe for this website to take most up-to-date updates, thus where can i do it
please help.
Hello very nice blog!! Man .. Excellent .. Amazing ..
I’ll bookmark your site and take the feeds also? I’m happy to seek out a lot of useful info right
here within the put up, we want develop
extra techniques on this regard, thank you for
sharing. . . . . .
When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a
comment is added I get four e-mails with the same comment.
Is there any way you can remove me from that service?
Thanks a lot!
Its like you learn my thoughts! You appear to understand so much about this, like you wrote
the guide in it or something. I feel that you can do with a
few percent to pressure the message home a bit, but instead of that, that is fantastic blog.
An excellent read. I will definitely be back.
That is a great tip especially to those fresh to the blogosphere.
Short but very precise information… Appreciate your sharing
this one. A must read post!