Conditions in Java Script :
We will start this article with Conditions in Java Script :
If-else Condition-
It is a type of condition where it will execute a block of code when the condition in the if statement is true. If the condition is false then the else block will be executed.
Syntax-
if (condition is true) {
code is executed
}
else {
code is executed
}
Let us see with an example :
let age=3;
if(age<=5){
console.log('Welcome to Primary Kids Section');
}
else{
console.log('Welcome To Upper Primary Kids SEction');
}
The output is :
Welcome to Primary Kids Section
Here in this case condition is evaluated to true so it goes towards the if statement.
Now if the condition is evaluated to false then
let age=6;
if(age<=5){
console.log('Welcome to Primary Kids Section');
}
else{
console.log('Welcome To Upper Primary Kids Section');
}
In this case, the output is :
Welcome To Upper Primary Kids Section
Else-if Condition-
Now in this case we have to check multiple statements so else if condition is used. Here when the if statement is false then it will move into the else-if statement. Now if that is also false then it will move into the else statement.
Let us Understand with an example :
let cricketer='Sachin';
if (cricketer=='Sachin'){
console.log('God of Cricket');
}
else if (cricketer=='Virat'){
console.log('Run Machine');
}
else if(cricketer=='Rahul'){
console.log('The Wall');
}
else{
console.log('Not a Cricketer');
}
Now Here in this case the first statement is true so it does not move into the else-if condition.
The output is :
God of Cricket
Here if the first statement is false then :
let cricketer='Virat';
if (cricketer=='Sachin'){
console.log('God of Cricket');
}
else if (cricketer=='Virat'){
console.log('Run Machine');
}
else if(cricketer=='Rahul'){
console.log('The Wall');
}
else{
console.log('Not a Cricketer');
}
Now here the second condition becomes true so the second statement is executed.
The output is :
Run Machine
Switch- Case -
There will be some cases in javascript where we will use the switch-case over else-if but in most of the cases we are going to use the else-if condition. Here in the case of, switch-case the switch statement evaluates a variable/expression inside the parenthesis.
If the result of the expression is equal to the first case then the first block will be executed and if the result of the expression is equal to the second case then the second block will be executed and the same thing goes on and at last, the default block will be executed.
Now let us understand the switch case with an example :
let sports='Cricket';
switch(sports){
case 'Cricket':
console.log('Welcome to Game of Cricket');
break;
case 'Football':
console.log('Welcome to Game of Football');
break;
case 'Hockey':
console.log('Welcome to Game of Hockey');
break;
default:
console.log('Other Sports');
}
Now in this case the first case becomes true so the first block of code is executed.
The Output is :
Welcome to Game of Cricket
If the second case becomes true then-
let sports='Football';
switch(sports){
case 'Cricket':
console.log('Welcome to Game of Cricket');
break;
case 'Football':
console.log('Welcome to Game of Football');
break;
case 'Hockey':
console.log('Welcome to Game of Hockey');
break;
default:
console.log('Other Sports');
}
Here in this case the second case becomes true so the second statement is executed.
The output is :
Welcome to Game of Football
Now in both cases, we have written a keyword break if we do not use the keyword break then what would be the outcome :
let sports='Cricket';
switch(sports){
case 'Cricket':
console.log('Welcome to Game of Cricket');
case 'Football':
console.log('Welcome to Game of Football');
break;
case 'Hockey':
console.log('Welcome to Game of Hockey');
break;
default:
console.log('Other Sports');
}
Here we can see the result the first case becomes true the first statement is executed but then also it jumps onto the second block of code and the second statement is also executed. The output is :
Welcome to Game of Cricket
Welcome to Game of Football
Ternary Operator/Ternary Condition-
The Ternary Operator is a Conditional Operator which evaluates either of two expressions a true expression or a false expression.
Syntax :
condition ? trueExpression : falseExpression
As in the above syntax, the ternary operator is created by two operators ? and :
After ? the true expression and after : the false expression. It is mostly used for checking in user login and logout option.
Now let us understand with an example:
let userlogin=true;
userlogin ? console.log('logout') : console.log('login');
Here we can see the user login is true which means the user is logged in so it will display a logout. The Output is :
logout
Now if the user login becomes false or the user is not logged in then :
let userlogin=false;
userlogin ? console.log('logout') : console.log('login');
Here we can see the result it is displaying the login message. The Output is :
login
DATE MATH RANDOM POWER :
Unlike other operators in JavaScript, there are some other operators which are date, math random and power.
Date() :
JavaScript provides a date object that gives us the date and time in your code.
The date object is used to work with dates and times and JavaScript. It can create a new Date object by calling the Date() constructor to get the current date and time.
The date() object has several methods let us explore these methods.
const now=new Date();
console.log(now);
Here now is a variable it will assign memory to something. The Date is a method. It gives us the current date and time which fetches out from your system.
The output is-
2023-01-19T05:46:23.573Z
To String()-
It gives the date and time in a string format.
const now=new Date();
console.log(now.toString());
The output is :
Thu Jan 19 2023 11:23:42 GMT+0530 (India Standard Time)
getFullYear()-
By this method the full Year is displayed.
const now=new Date();
console.log(now.getFullYear());
The output is :
2023
newDate()-
We can also set the full date month and year by our own choice,let us see how we can achieve this
let newdate= new Date('2030-02-02');
console.log(newdate);
The output is :
2030-02-02T00:00:00.000Z
getMonth()-
We can get the current month by this method-it displays the month,from 0-11 according to local time.
const now=new Date();
console.log(now.getMonth());
The output is :
0
getDay()-
We can get the Day of the week for 0-6 according to local time.
const now=new Date();
console.log(now.getDay());
The Output is :
4
getDate()-
Gets the day of the month (1–31) according to local time.
const now=new Date();
console.log(now.getDate());
The Output is :
19
getTime()-
It gives the time according to the local system
const now=new Date();
console.log(now.getTime());
The Output is :
1674109175344
getHours()-
Gets the hour from 0 to 23 according to local time.
const now=new Date();
console.log(now.getHours());
The Output is :
11
getMinutes()-
Gets the minute from 0 to 59 according to local time.
const now=new Date();
console.log(now.getMinutes());
The Output is :
54
getSeconds()-
Gets Seconds according to the local Time.
const now=new Date();
console.log(now.getSeconds());
The Output is :
58
getUTCDate()-
Gets the day of the month (1–31) according to universal time.
const now=new Date();
console.log(now.getUTCDate());
The Output is :
19
Math()-
The Math object provides properties and methods for mathematical constants and functions.
Math.round()-
This method is used to round a number to the nearest integer.
let num=3.2;
console.log(Math.round(3.2));
console.log(Math.round(9.5));
console.log(Math.round(9.2));
In the First case, it makes the number to the nearest integer to 3. In the last two cases we have seen the number being rounded to its nearest integer which is 9.5 becomes 10 and 9.2 becomes 9.
Math.floor()-
By this method, the smallest number is displayed.
const PI=Math;
console.log(Math.floor(9.5));
It returns the value 9 from 9.5. The Output is :
9
Math.ceil()-
By this method, the greater number is displayed.
const PI=Math;
console.log(Math.ceil(9.5));
It returns the value 10 from 9.5. The Output is :
10
Math.trunc()-
By this method, the digits after the integer are removed.
const PI=Math;
console.log(Math.trunc(5.678));
So we can see the digits after 5 are displayed. The output is :
5
Math.sign()-
By this method, it returns 1 for the +ve number and -1 for the -ve number.
const PI=Math;
console.log(Math.sign(489));
console.log(Math.sign(-489));
The Output is :
1
-1
Math.sqrt()-
By this method, it gives us the square root.
const PI=Math;
console.log(Math.sqrt(144));
It returns 12 which is the square root of 144. The Output is :
12
Math.abs()-
By this method, we get a positive number.
const PI=Math;
console.log(Math.abs(-20));
It returns 20 from -20 which is a positive number. The Output is :
20
Math.random()-
By this method, we get the random number.
const PI=Math;
console.log(Math.random());
console.log(Math.random()*10);
console.log(Math.random()*50);
From the first case, we get a number between 0 and 0.9009.
In the second case. it will generate a number from 0 to 10.
In the third case. it will generate a number from 0 to 50.
The Outputs are :
0.7078338076443456
3.7280360247800703
13.075045698983823
Math.pow()-
By this method, it will raise the power.
const PI=Math;
console.log(Math.pow(5,2));
It returns the number 5*5=25.
25
Math.max()-
This method is used to find the maximum number from a set of numbers.
let num=Math.max(20,30,50,60,90);
console.log(num);
The output is :
90
Math.min()-
This method is used to find the minimum number from a set of numbers.
let num1=Math.min(20,30,50,60,90);
console.log(num1);
The Output is :
20
Loops in JavaScript :
Loops are used in programming for the repeatation. There are different types of loops in javascript- let us understand them
While Loop-
Syntax-
while ( condition){
statements;
//body of loop
}
The condition appears at the start of the loop.
While Loop is also known as entry controlled loop.
There is no condition at the end of the loop.
If the condition inside the loop becomes true then the code inside the loop is executed.
The condition is evaluated again and when the condition becomes false then the loop stops.
Let's understand the while loop with an example :
let num=5;
while(num<=10)
{
console.log('Value of num is' , num);
num++;
}
Here the value of num is set to 5 and the condition is checked whether the num is less than or equal to 10 and as soon as the num value reaches to 10 the loop stops, here the num++ is the increment. So the output achieved is.
Value of num is 5
Value of num is 6
Value of num is 7
Value of num is 8
Value of num is 9
Value of num is 10
Do-While Loop :
Syntax :
do {
statements;
// body of loop.
}
while( Condition );
The condition is present at the end of the loop.
Do-While Loop is also known as exit controlled loop.
There is a condition at the end of the loop.
During the first condition check of the loop if the condition is false then also the loop will be executed once.
If the condition is true then the loop continues untill the loop condition ends.
Let's understand the while loop with an example :
let num1=5
do{
console.log('Value of num is ',num1);
num1++;
}while(num1<=10);
Here the output is same as the while loop because the condition is the same.
The Output is :
Value of num is 5
Value of num is 6
Value of num is 7
Value of num is 8
Value of num is 9
Value of num is 10
Difference between While Loop and Do-While Loop :
Let us understand this with an example :
let i=11;
while(i<=10){
console.log('Value of i is',i);
i++;
}
Here the condition does not satisfy so the condition is not checked once so in the output there is nothing. But on the other hand, let us see this in a do-while loop.
let i=11;
do{
console.log('Value of i is',i);
i++;
}while(i<=10);
Here also the condition becomes false but the code will go inside the loop once and it will print the value of i then as soon as the condition is checked then the loop ends. The Output is :
Value of i is 11
For Loop-
Syntax :
for(initialization; condition; increment/derement){
body of the 'for' loop
}
The initialization, condition checking and increment decrement are done at the beginning of the statement.
If the condition is not mentioned in the for loop then it will go infinite times.
The initialization is done once and it is not repeated.
Let us understand for loop with an example :
for(let i=0;i<=11;i++)
{
console.log('Value of i is',i );
}
The Output of the above is
Value of i is 0
Value of i is 1
Value of i is 2
Value of i is 3
Value of i is 4
Value of i is 5
Value of i is 6
Value of i is 7
Value of i is 8
Value of i is 9
Value of i is 10
Value of i is 11
Break and Continue :
Break :
It simply means terminating everything and coming out of the loop.
for(let i=0;i<=5;i++){
if(i==3){
break;
}
console.log(i);
}
So here in this case the loop will break as soon as it will reach the condition i==3 and it prints from 0 to 2. The Output of the above is :
0
1
2
Continue :
It simply means skip and continue.
for(let i=0;i<=5;i++){
if(i==3){
continue;
}
console.log(i);
}
Now here in this case it will print all the values from 0 to 5 means it will skip the value 3 and the output is :
0
1
2
4
5
For of Loop :
For of loop is used in an array in simple words it iterates over an array.
Let us understand this with an example :
let cricketers=['Kapil','Sachin','Virat','Rohit','Surya'];
let uppercase=[];
for(let top of cricketrs){
uppercase.push(top.toUpperCase());
}
console.log(uppercase);
Here first an array is declared with the name cricketers which contains 5 values. Then an empty array is declared named uppercase. With the help of for of loop and to uppercase method all the values in the array named cricketers are then shifted towards the empty array named uppercase with all the values having capital letters. The Output of the above is :
[ 'KAPIL', 'SACHIN', 'VIRAT', 'ROHIT', 'SURYA' ]