Table of contents
No headings in the article.
Switch statement will have a variable that contains a value initially. The switch statement will try to match the value in the variable with the case it has and then the statement associated with the case will be executed, if the case matches the value of the variable.
Example: In this example, it used a hardcoded value.
const day="Monday" // variable *day* has value *monday*
// and it is stored in a *const* type variable.
switch (day) {
// <- varaible with value
case "monday":
// <- if variable matches with the case then the
// consoled value will be executed.
console.log("code");
console.log("go for ride");
break;
case "tuesday":
console.log("practice");
break;
case "wednesday":
case "thursday":
console.log("presentation");
break;
case "friday":
console.log("visit");
break;
case "saturday":
case " sunday":
console.log("enjoy weekand");
break;
default:
console.log("invalid input!");
break;
}
What are breaks?
The "break" plays an important role, it does not allow the process of execution to go to the next line. Without "break" all the cases and statements will be executed.
What are defaults ?
The default acts like "else" (same as if-else if condition).
What are prompts ?
The prompts() are dialogue boxes that are displayed in the browser, which ask for input from the user. One thing is to keep in mind always, that the input taken from the user through prompt is always a string, that is in the case of numbers also.
*The good practice is to check for the value, which is taken from the user input process always using typeof
.
We can also take input from the user like this:-
Example : let day = prompt("enter the day :");
The above program can be written using if-else if statements:-
Example:
const day = "monday";
if (!day) {
console.log("input field is blank");
} else if (day === "monday") {
console.log("code");
console.log("go for ride");
} else if (day === "tuesday") {
console.log("practice");
} else if (day === "wednesday" || day === "thursday") {
console.log("presentation");
} else if (day === "friday") {
console.log("visit");
} else if (day === "saturday" || day === "sunday") {
console.log("enjoy weekend!");
} else {
console.log("enter a valid input!");
}
What are if-else and if-else if statement?
The if-else
statement is basically a group of conditions that can be true
or false
, on basis of conditions the particular statements will get executed.
Example : Using if-else.
const rohansAge = 18;
if (rohansAge >= 18) {
console.log("Rohan is eligible of driving licence.");
} else {
console.log("Rohan is not eligible for driving licence.");
}
Example : Using if-else if.
const billAmount = 250;
if (billAmount < 300) {
console.log("eligible for 10% discount");
} else if (billAmount > 300) {
console.log("eligible for 20% discount");
} else {
console.log('not eligible for discounts')
}
*JS don't have elseif
syntax. However, we can write it with a space between else
and if
:
What are truthy and falsy values?
The JS has 5 falsy values. If we try to convert them into boolean it will become false. Anything other than these values will be true if we convert them into boolean.
These are 5 falsy values:-
0 - zero , ' ' -> empty string , undefined, null , NaN.
what are Expressions ?
Expression is a part code which produces a value.Example:
3+4
1992
or true || false , true && false
.
what are Statements ?
Statements are a bigger part of code that does not produce any value instead it performs some actions and declares some variable.Example:
if(i%3===0 && i%5===0 ){
console.log('fizzBuzz');
}else if (i%3===0){
console.log('fizz');
}
Read more about Statements and there types of statements
*JavaScript expects expressions and statements depending on the situation.
Conclusion
I have liked using switched statement over if-else if statement. The Switch is very easily understood then if-else if statement. It is people's choice which one they want to use.JavaScript Fundamentals #2 That is what I have learned about switch statements, if-else if and some of the syntax related to these statements. coming up with the next blog JavaScript Fundamentals #3.