#3 Operators + Conditional Statements | Javascript Hindi Course For Beginners ( 2023 )
Operators
Arithmetic Operators
Name | Symbol |
Addition | + |
Subtraction | - |
Multiplication | * |
Division | / |
Modules | % |
Power | ** |
Increment | ++ |
Decrement | - - |
Assignment Operators
Shortcut | Full Expression |
= | a = b |
+= | a = a + b |
-= | a = a - b |
*= | a = a * b |
/= | a = a/b |
**= | a = a**b |
Comparison Operators
Symbols | Explanation |
== | Equal to |
!= | Not equal to |
=== | Strict equal to ( type checking also ) |
!== | Strict not equal to ( type checking also ) |
< | Less than |
> | Greater than |
<= | Less than and equal to |
>= | Greater than and equal to |
Logical Operators
Symbols | Explanation |
&& | Logical AND |
|| | Logical OR |
! | NOT |
AND Logic ( && )
Value 1 | Value 2 | Output |
true | true | true |
true | false | false |
false | true | false |
false | false | false |
OR Logic ( && )
Value 1 | Value 2 | Output |
true | true | true |
true | false | true |
false | true | true |
false | false | false |
NOT Logic ( ! )
Value 1 | Output |
true | false |
false | true |
Bitwise operators perform bit-level operations on numbers. They are used to manipulate individual bits in an integer. You can skip this topic .
Conditional Expressions
conditional statements are used to make decisions based on whether a condition is true or false. The most commonly used conditional statements are:
- if statement: Executes a block of code only if a specified condition is true.
- if...else statement: Executes a block of code if the condition is true, and another block of code if the condition is false.
- switch statement: Selects one of many blocks of code to be executed based on multiple conditions.
if statement
How if condition works
Lets see if you want check certain condition, if that condition meets requirements then you want to execute that code.
if statement get condition
true
then it executes is code.if(Any Condition) { // if condition is true // Exceute this code }
//if Example const score = 91; if( score > 90 ){ console.log("You are topper"); } // look this example // when score is great than 90 then our code will be executed // if not then the code block will be skipped
if else statements
How if else statements works
See When if Statement code is not executed then else block is automatically get executed,
else block provide fallback case. if any thing went wrong then else block will get executed
if(Condition) { // if Condition is true }else{ // if Condition is false }
//if else Example const score = 90; if( score > 90 ){ console.log("You are topper"); } else{ console.log("You are average student"); } // look this example // when score is great than 90 then our code will be executed // if statement is not executed then else block will be executed // here value is 90 -> hence else block will be executed
else if statements
In some cased you need to check multiple condition , in that situation you need to use
else if
statements.if(Condition) { // if Condition is true , code will be executed } else if(another condition ) { // if another condition is true , code will be executed } else{ // if no condtion is true this code will be executed }
// else if Example const score = 50; if( score > 90 ){ console.log("You are topper"); } else if (score > 45) { console.log("You are average student"); } else{ console.log("You are not passed"); } // look this example // here we are checking two condition when // in first condtion we are check is score is greater than 90 // then we will log "You are topper" // if that condition is not met then we will check another condition // we will check if score is greater than 45 than we will log // "You are average student" // if no case will be true then else block will be get excuted
Switch Statements
A switch statement in JavaScript is a conditional statement that tests an expression against multiple cases and executes the code associated with the first matching case. If no cases match, a default case can be specified to execute. The switch statement provides a cleaner syntax and improved readability compared to a series of if-else statements.
switch (expression) { case value1: //Statements executed when the //result of expression matches value1 break; case value2: //Statements executed when the //result of expression matches value2 break; case valueN: //Statements executed when the //result of expression matches valueN break; default: //Statements executed when none of //the values match the value of the expression break; }
// switch Example 1 const num1 = 5; const num2 = 8; const operation = "+"; let result; switch (operation) { case "+": result = num1 + num2; break; case "-": result = num1 - num2; break; case "*": result = num1 * num2; break; case "/": result = num1 / num2; break; default: result = "Invalid operator"; break; } // example 2 // alternative of else if statements const score = 55; switch (true) { case score > 90: console.log("You are topper"); break; case score > 80: console.log("Well Done, But you are not topper"); break; case score > 45: console.log("You are average student"); break; default: console.log("You are not passed"); break; }
Shortcut of if Else - Ternary Operator
condition ? exprIfTrue : exprIfFalse
//if else Example const score = 90; if( score > 90 ){ console.log("You are topper"); } else{ console.log("you are not topper");; } // lets use ternary operator here score > 90 ? console.log("You are topper") : console.log("you are not topper");; // output of both cases will be equal // which one we need to use // if else provides better readability // so don't use ternary every where.
Questions | Solve them
- Write a code to check person is adult or not
- Write a code to check number is even or odd
- Using Switch Statement Check number is divisible by 5 and 15
Beginner's javascript Course in Hindi | 2023 | Javascript Course for Beginners in Hindi Complete Notes of JS - https://dosomecoding.com/courses/javascript/javascript-hindi-course-for-beginners Javascript Beginners Playlist Link - https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS Linkedin - https://linkedin.com/in/anshuopinion Telegram Channel - https://telegram.me/dosomecodinghelp Instagram - https://instagram.com/dosomecoding Github - https://github.com/anshuopinion HTML Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gL2isb9Kr1GvTM8id2gdtY CSS Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gWK5TVILnKSvuc9Fc15sbH Html and CSS practice Projects https://www.youtube.com/playlist?list=PLPppPPmk0i3hZCNmbVtcP1hlwDKOdUFX9 Javascript Course https://www.youtube.com/playlist?list=PLPppPPmk0i3gZCY8JZ0H5oykFGevvNzNS Linkedin - https://linkedin.com/in/anshuopinion Telegram Channel - https://telegram.me/dosomecodinghelp Instagram - https://instagram.com/dosomecoding Github - https://github.com/anshuopinion