Conditionals in JavaScript

If you’ve a little bit of experience with programming, then you must be aware of that coding is mostly about decision making – doing this if something is true or else doing other stuff if its not.

In JavaScript Programming Language for having Control Flow across code, it is divided into blocks with Conditionals and depending upon conditional’s values certain blocks are executed while certain are not.

Following is a list of all conditionals in JavaScript –
if
• if else
• else if
• switch

if JavaScript Conditional

if conditional in JavaScript allows code to be executed conditionally based up whether expression with if evaluates to true or not.
Common syntax for if conditional is –

if (expression)
     statement

// Not only a single statement, you could also have a compound statement
if (expression)
     statement
     statement

if (x == 10)
     console.log("Computer Science Hub");

if (x == 10)
     console.log("Hello, your viewing");
     console.log("Computer Science Hub");

Not only you can have just one condition for if statement rather you could also state multiple conditions as well. Below is an example of multiple conditions for javaScript’s if conditional

if (x == 10 || x == 13){
     console.log("Hello");
}

In this example code, if x value is either of 10 or 13 then 
"Hello" will be printed.

if else statement/conditonal JavaScript

if else conditional in JavaScript is used for decision making whether some code block will be executed or an other code block will be, based upon whether expression evaluates to true or false.

if (expression)
    statement
else
    statement

//Inspite of having single statements, you could have multiple as well
if (expression)
    statement
    statement
else
    statement
    statement

if (x == 10)
     console.log("Computer Science Hub");
else
     console.log("Google")

if (x == 10)
     console.log("Hello, your viewing");
     console.log("Computer Science Hub");
else
     console.log("Google");
     console.log("I am using Google");

else if JavaScript Conditional

While JavaScript’s if else conditional computes an expression and then picks up out of two available code blocks to execute, else if conditional computes multiple expressions and based upon that execute one of many code blocks.

Genral sytax for else if –
if (expression1){
statement1
}
else if (expression2){
statement2
}
else if (expression3){
statement3
}
else{
statement4
}

if (n == 1){
	console.log("Google");
}
else if (n == 2){
	console.log("Facebook");
}
else if (n == 3){
	console.log("Microsoft");
}
else{
	console.log("Just a technology company");
}

switch Statement/Conditional in JavaScript

All of other three JavaScript Conditionals if, if else, else if evaluates expressions and based upon value returned there picks up a code block to be executed. But switch statement is completely different, first an expression value is computed and then compared with each case value, if its same then that specific code block is executed and then JavaScript Engine moves to next code statement(not to next case statement inside switch).

Synatx for switch statement –

switch (expression) {
             case value1:
                  statement1
                  break;
             case value2:
                  statement2
                  break;
             default:
                  statement3
                  break;
}

Below is an code example of JavaScript switch statement –

switch(expression){
	case value1:
		console.log("Google");
		break;

	case value2:
		console.log("Facebook");
		break;

	default:
		console.log("A tech company");
		break;
}

Conclusion

Conditionals are quite easy to use in JavaScript, one thing that you as a JS Developer should be careful about is use of curly brackets for enclosing statement blocks. If your not doing then, it’s still fine but doing so would improve code readability and also would be helpful for other people to understand your code.

Moreover if in case, if your learning javaScript programming language then I’ve put together many amazing articles about JS on this website, you check those out here – JavaScript Computer Science Hub.

Gagan

Hi, there I'm founder of ComputerScienceHub(Started this to bring useful Computer Science information just at one place). Personally I've been doing JavaScript, Python development since 2015(Been long) - Worked upon couple of Web Development Projects, Did some Data Science stuff using Python. Nowadays primarily I work as Freelance JavaScript Developer(Web Developer) and on side-by-side managing team of Computer Science specialists at ComputerScienceHub.io

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Posts