Conditionals
- Conditional statements are used for making decisions.
- JavaScript script executed sequentially from top to bottom.
If the processing logic requires so, the sequential flow of execution can be altered in two ways:
- Conditional execution: a block of one or more statements will be executed if a certain expression is true
- Repetitive execution: a block of one or more statements will be repetitively executed as long as a certain expression is true.
Conditions can be implemented using the following ways
- if
- if else
- if else if
- switch
- ternary operator
if
- if is to be used check if a condition is true and execute the block code.
- To create an if condition, we need an if keyword, a condition inside a parenthesis, and a block of code inside a curly bracket({})
// syntax
if (condition) {
//this part of code runs for true condition
}
let num = 3
if (num > 0) {
console.log(`${num} is a positive number`)
}
// 3 is a positive number
explanations:
- 3 is greater than 0, so it is a positive number
- The condition was true and the block of code was executed
- if the condition is false, we won't see any results.
if - else
- If the condition is true the first block will be executed, if not then the else block will be executed.
// syntax
if (condition) {
//This part of the code runs for true condition
} else {
//This part of the code runs for false condition
}
Example:
let num = 3
if (num > 0) {
console.log(`${num} is a positive number)
} else {
console.log(`${num} is a negative number`)
}
// 3 is a positive number
num = -3
if (num > 0) {
console.log(`${num} is a positive number`)
} else {
console.log(`${num} is a negative number`)
}
// -3 is a negative number
if else if
- We use else if when we have multiple conditions
// syntax
if (condition) {
// code
} else if (condition) {
// code
} else {
// code
}
let a = 0
if (a > 0) {
console.log(`${a} is a positive number`)
} else if (a < 0) {
console.log(`${a} is a negative number`)
} else if (a == 0) {
console.log(`${a} is zero`)
} else {
console.log(`${a} is not a number`)
}
Switch
- A switch is an alternative for Nested if - else
- The switch statement starts with a switch keyword followed by a parenthesis and code block
- Inside the code block, we will have different cases
- Case block runs if the value in the switch statement parenthesis matches with the case value
- The break statement is to terminate execution so the code execution does not go down after the condition is satisfied
- The default block runs if all the cases don't satisfy the condition.
// switch
switch (caseValue) {
case 1:
// code
break
case 2:
// code
break
case 3:
// code
break
default:
// code
}
// Switch More Examples
let dayUserInput = prompt('What day is today ?')
let day = dayUserInput.toLowerCase()
switch (day) {
case 'Monday':
console.log('Today is Monday')
break
case 'Tuesday':
console.log('Today is Tuesday')
break
case 'Wednesday':
console.log('Today is Wednesday')
break
case 'Thursday':
console.log('Today is Thursday')
break
case 'Friday':
console.log('Today is Friday')
break
case 'Saturday':
console.log('Today is Saturday')
break
case 'Sunday':
console.log('Today is Sunday')
break
default:
console.log('It is not a week day.')
}
Ternary Operators
let age = prompt('Enter number');
age > 20
? console.log('You need a rain coat.')
: console.log('No need for a rain coat.')
0 Comments:
Post a Comment
Do leave your comments