Conditional statements in javascript

preview_player
Показать описание
Link for all dot net and sql server video tutorial playlists

Link for slides, code samples and text version of the video

Healthy diet is very important both for the body and mind. If you like Aarvi Kitchen recipes, please support by sharing, subscribing and liking our YouTube channel. Hope you can help.

JavaScript code is executed in a linear fashion from the first line to the last line. If for some reason you want to interrupt this flow and execute certain statements, only, if certain condition is met, then we use conditional statements.

JavaScript has the following conditional statements
if
if else
if else if else
switch
ternary operator - shortcut for an if...else statement

If example
var userInput = Number(prompt("Please enter a number", ""));
if (userInput == 1)
{
alert("You number is One");
}
if (userInput == 2)
{
alert("You number is Two");
}
if (userInput == 3)
{
alert("Your number is Three");
}
if(userInput != 1 && userInput != 2 && userInput != 3)
{
alert("Your number is not between 1 and 3");
}

If else if example
var userInput = Number(prompt("Please enter a number", ""));
if (userInput == 1)
{
alert("You number is One");
}
else if (userInput == 2)
{
alert("You number is Two");
}
else if (userInput == 3)
{
alert("Your number is Three");
}
else
{
alert("Your number is not between 1 and 3");
}
Рекомендации по теме
Комментарии
Автор

nice tutorial, your language is easy to understand.

shirinsultana
Автор

very good explanation of why to use else and why not repeated if.thank you

happyplaces
Автор

These tutorials are amazing. Keep it up!

zecron
Автор

nice tutorial! thanks it really helped me alot.

mehdihussainseo
Автор

gr8 tutorial thanks.Did not know there was something like prompt to accept user input

vmohakrish
Автор

even though this vid is uploaded long time ago it help me a lot thanks for the
:)

yuriell_
Автор

Number () is new to me. We also have parseInt() n parseFloat () for the same. Correct me if am wrong!

computer_programming_gk