(lt.32)Unraveling JavaScript Loops: Master the Art of Iteration for Efficient Coding!
himanshu
2 min read
Table of contents
If else :
In js the else part doesn't take any condition but there will be no error if you put condition in else part, but it can result to uneven results:
// let a= prompt("u r age : ") // to enter a value
// console.log(typeof a) // used to know the datatype of a
// a = Number.parseInt(a) // used to convert data type to number
// console.log(typeof a)
// if(a>0)
// {
// console.log("valid age")
// }
// else
// {
// console.log("not valid")
// }
//
// else case with condition
let a ="hii"
if( a== "he")
{
console.log("he")
}
else if( a== "she")
{
console.log("she")
}
else if( a== "hii")
{
console.log("hii")
}
else( a== "me")
{
console.log("me")
}
switch case: It is a selection control statement.
let n = "gkd";
switch (n) {
case "hd":
console.log("himanshu");
break;
case "gkd":
console.log("gopal");
break;
default:
console.log("nothing");
}
For loop:
for(let i=0 ; i<=5;i++)
{
console.log(i)
}
// program to add number
let sum = 0
let n= prompt('enter any no : ')
n =Number.parseInt(n); // here you have ability to skip this conversion but it
//is necessary to change it as n is a string by default here and hence can lead
// to the unrequired results
for(let i=0;i<=n;i++)
{
sum += i
}
console.log("the sum is ")
console.log(sum)
While loop:
// let n = prompt("enter number ")
// //n = Number.parseInt(n)
// let i =0
// while(i<=n)
// {
// console.log(i)
// i++
// }
dowhile loop:
let a = 3;
do{
console.log("hii")
}while(a>5)
ternary operator:
// let age = prompt("enetr age")
// age = Number.parseInt(age)
// // console.log(age>18?'valid age':'invalid age')
// // or
// age>18?console.log("can vote"):console.log("cant vote")
lt.31link: https://hashnode.com/post/cltenwn7m00020al78z6p0zma
10
Subscribe to my newsletter
Read articles from himanshu directly inside your inbox. Subscribe to the newsletter, and don't miss out.
while loop javasciptternary operator javascriptConditional statementjsJavaScriptNode.jsWeb Developmentwebdevfor loop in javascript
Written by