Code Skiller logoCB Logo
Logo LearnLearnLogo PracticePracticeLogo HireHireLogo IDEIDE

Conditional Statements

User image

Published by

sanya sanya

Published at: 6th May, 2023
1.595 mins read

The statements which perform as per the given specific conditions are known as Conditional Statements. There are four types of conditional statements in Javascript namely -

  • If Condition
  • Else Condition
  • Else If Condition
  • Switch Condition

If Statement

The If statement specifies a block executed after providing a specific condition. In simple words, if a condition is found to be true, the block of code will be executed.

The code showing the If Condition is mentioned below -

let a = 10; if(a > 5) { console.log("Hello World); } //Output: Hello World

Else Statement

The Else Statement specifies a block that will be executed when a certain condition gets false. The code showing the else condition is mentioned below -

let a = 10; if(a > 50) { console.log("Hello World); } else { console.log("Java Script is best") } //Output: Hello World

Else-If Statement

The Else-if Condition specifies the order, If condition, then else if condition, and then finally else condition.

The code showing the Else-if Condition is mentioned below -

let age = 8; if(age < 18){ console.log("You cannot drive"); } else if(age == 18) { console.log("You must have a DL); } else { console.log("You cannot drive"); } //output: You cannot drive

For instance, John goes to a restaurant and he's so hungry that he wanted to eat something without waiting. The waiter arrives and ask for the order, John hurriedly said water, bring me pizza, if pizza is not available, then burger, and if burger is also not available, then Maggi.

Many a time, in the code, there are various if-else-if conditions arrives, which is also known as the if-else ladder.

There is a simple use of ternary operators in the Conditional Statements which is also considered as the short-hand if-else and the code for the same is mentioned below -

return condition? A: B;

Library

WEB DEVELOPMENT

Basic

HTML - Hyper Text Markup Language

CSS - Cascading Style Sheets

JavaScript

An Introduction to Javascript!

How to Run JavaScript Code

Variables in Javascript

Numbers in JavaScript

JavaScript Operators

Data Types in JavaScript

Conditional Statements

Switch Statements

Loops in Javascript

Arrays in JavaScript

Strings in JavaScript

Objects in JavaScript

Object Methods in JavaScript

Functions in JavaScript

Object Referencing and Copying in JavaScript

' this' keyword

Asynchronous Programming in JavaScript

Callbacks in JavaScript

Promises in JavaScript

Constructor Functions in JavaScript

Async and Await in JavaScript

Type Conversion in Javascript

DOM

Currying in JavaScript

Network Request

Frontend

Backend

Interview Questions

FAANG QUESTIONS

On this page

If Statement

Else Statement

Else-If Statement