JavaScript Conditional Statements Introduction

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

Welcome to the JavaScript documentation! This lab will give you an introduction to conditionals.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/cond_stmts("`Conditional Statements`") javascript/ToolsandEnvironmentGroup -.-> javascript/debugging("`Debugging`") subgraph Lab Skills javascript/variables -.-> lab-106900{{"`JavaScript Conditional Statements Introduction`"}} javascript/data_types -.-> lab-106900{{"`JavaScript Conditional Statements Introduction`"}} javascript/comp_ops -.-> lab-106900{{"`JavaScript Conditional Statements Introduction`"}} javascript/cond_stmts -.-> lab-106900{{"`JavaScript Conditional Statements Introduction`"}} javascript/debugging -.-> lab-106900{{"`JavaScript Conditional Statements Introduction`"}} end

Conditionals

Open the Terminal/SSH and type node to start practicing coding.

Conditionals are code structures used to test if an expression returns true or not. A very common form of conditionals is the if...else statement. For example:

let iceCream = "chocolate";
if (iceCream === "chocolate") {
  console.log("Yay, I love chocolate ice cream!");
} else {
  console.log("Awwww, but chocolate is my favorite…");
}

The expression inside the if () is the test. This uses the strict equality operator (as described above) to compare the variable iceCream with the string chocolate to see if the two are equal. If this comparison returns true, the first block of code runs. If the comparison is not true, the second block of code—after the else statement—runs instead.

Summary

Congratulations! You have completed the Conditionals lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like