Posts

Showing posts with the label JavaScript Conditinal Statement

Nullish coalescing operator '??'

What is the Nullish coalescing operator '??' in JavaScript? The Nullish coalescing operator '??' is a new addition to JavaScript introduced in ECMAScript 2020. It is used to provide a default value when the value on the left-hand side of the operator is null or undefined. The '??' operator can be seen as a shorthand way of writing a conditional statement to check for null or undefined values and provide a default value in such cases. Syntax The syntax of the Nullish coalescing operator is as follows: valueToCheck ?? defaultValue valueToCheck ?? defaultValue   result = a ?? b ; The result of a ?? b is: if a is defined, then a, if a isn’t defined, then b. We can rewrite result = a ?? b using the operators that we already know, like this: result = ( a !== null && a !== undefined ) ? a : b ; It means that, the express...

Switch Statements

JavaScript Switch Statements Switch statements in JavaScript are a type of conditional statement that allow you to check the value of an expression and perform different actions based on different cases. Syntax The basic syntax of a switch statement is as follows: switch ( expression ) { case value1 : // code block to be executed if expression matches value1 break ; case value2 : // code block to be executed if expression matches value2 break ; ... default : // code block to be executed if expression doesn't match any of the cases } In a switch statement, the `expression` is evaluated once, and its value is compared with the values of each `case`. If there is a match, the corresponding code block is executed until a `break` statement is encountered or until the end of the switch statement. If no match is found, the code block under the `default` case (if presen...

Ternary operator

JavaScript Ternary Operator The JavaScript ternary operator is a concise way to write conditional statements in JavaScript. It is often used as a shorter alternative to if-else statements. Syntax The syntax of the JavaScript ternary operator is as follows: condition ? expression1 : expression2; The condition is evaluated, and if the condition is true, expression1 is executed. If the condition is false, expression2 is executed. Examples Here are a few examples of using the JavaScript ternary operator: Parentheses make the code more readable, so we recommend using them. const age = 22 ; const canVote = ( age >= 18 ) ? 'Yes' : 'No' ; console. log ( canVote ) ; // Output: Yes   const number = 7 ; const isEven = ( number % 2 === 0 ) ? true : false ; console. log ( isEven ) ; // Output: false   const greeting = 'Good ' + ( ( new Date ( ) . getHours ( ) < 12 ) ? 'morning' : 'afternoon' ) ; console. lo...

'if', 'if-else', 'if-else if' statements

Introduction In JavaScript, conditional statements are used to perform different actions based on different conditions. These statements allow the program to make decisions and execute different code blocks based on the outcome of those decisions. There are three main types of conditional statements in JavaScript: `if`, `if else`, and `if else if`. The `if` Statement The `if` statement is used to execute a block of code if a specified condition is true. Here is the syntax for the `if` statement: if ( condition ) { // code to be executed if the condition is true }   The `condition` can be any JavaScript expression that returns a boolean value (`true` or `false`). If the condition evaluates to `true`, the code block within the curly braces will be executed. Otherwise, the code block will be skipped. Example: let age = 18 ;   if ( age >= 18 ) { console. log ( "You are old enough to vote" ) ; } In this example, if the `age` variable is greater than or eq...

'break' & 'continue' statements

Introduction In JavaScript, the `break` and `continue` statements are used to control the flow of loops. They are often used inside loops to change the normal execution behavior. The `break` statement is used to exit a loop completely, while the `continue` statement is used to skip the current iteration of a loop and continue to the next iteration. These statements are particularly useful in situations where you need to terminate a loop prematurely or skip certain iterations based on certain conditions. The `break` Statement The `break` statement is used to exit a loop prematurely. When the `break` statement is encountered inside a loop, the loop immediately terminates and the program execution continues with the next statement after the loop. Here's a basic example that uses the `break` statement in a while loop: let i = 0 ;   while ( i < 10 ) { if ( i === 5 ) { break ; }   console. log ( i ) ; i ++; } In this example, the loop will execute ...