Introduction to JavaScript – Control Flow

Introduction to JavaScript – Control Flow

In this lesson, we’ll explore how we can use the building blocks of JavaScript to write programs that make decisions.

Control flow statements enable JavaScript programs to make decisions by executing code based on a condition. If a given condition is true, we execute one block of code. If the statement is false, we execute another block of code. For instance, if we were making a game in which the user had to choose which door to enter, we’d need a way for the program to know what to do once the user was in the next room.

Introduction to JavaScript – Control Flow

In this lesson, we’ll learn how to make decisions with JavaScript and how it can control the program’s flow.


let userName = 'Bernie'; //set your name in this variable.
let knowsJavaScript = false; //set this boolean to true or false and see results.

if (knowsJavaScript &&; userName) {
  console.log('Great, ' + userName + '! Get ready to practice your JavaScript!');
} else if (knowsJavaScript) {
  console.log('Great! Get ready to practice your JavaScript!');
} else if (userName) {
  console.log('Great, ' + userName + '! Get ready to learn something new!');
} else {
  console.log('Great! Get ready to learn something new!');
}

OUTPUT:

Great, Bernie! Get ready to learn something new!

Now follow this code: changed knowJavaScript = true

let userName = 'Bernie'; //set your name in this variable.
let knowsJavaScript = true; //set this boolean to true or false and see results.

if (knowsJavaScript && userName) {
  console.log('Great, ' + userName + '! Get ready to practice your JavaScript!');
} else if (knowsJavaScript) {
  console.log('Great! Get ready to practice your JavaScript!');
} else if (userName) {
  console.log('Great, ' + userName + '! Get ready to learn something new!');
} else {
  console.log('Great! Get ready to learn something new!');
}

OUTPUT:

Great, Bernie! Get ready to practice your JavaScript!

 


Related Videos:

MiltonMarketing.com Related Videos.

Related Links:

Online JavaScript Compiler. Code on the go.

See the JavaScript Glossary on if Statements

Control flow and error handling – Mozilla MDN web docs

Introduction to Python Programming Language

My little pony learning game in VB.NET

CSS, HTML, JAVASCRIPT, Online Compiler. Code on the go by replit.it

Introduction to JavaScript – Create a Variable: let

Introduction to JavaScript

Introduction to JavaScript – Variables: Review

Tips and Tricks for WRITING JAVASCRIPT code

Building a web page with HTML tags

Hello World Android app built with Android Studio

Learn about JavaScript IF STATEMENTS

How to make a Go-Back Input button with inline JavaScript

Introduction to JavaScript – Control Flow: True and False values

Introduction to JavaScript – Control Flow: if/else Statements

Who is this Android App Development course for?

Why do most sites use cookies?

FAQs

Connected through code, Choose Your Platform!

About the Author: Bernard Aybout

In the land of bytes and bits, a father of three sits, With a heart for tech and coding kits, in IT he never quits. At Magna's door, he took his stance, in Canada's wide expanse, At Karmax Heavy Stamping - Cosma's dance, he gave his career a chance. With a passion deep for teaching code, to the young minds he showed, The path where digital seeds are sowed, in critical thinking mode. But alas, not all was bright and fair, at Magna's lair, oh despair, Harassment, intimidation, a chilling air, made the workplace hard to bear. Management's maze and morale's dip, made our hero's spirit flip, In a demoralizing grip, his well-being began to slip. So he bid adieu to Magna's scene, from the division not so serene, Yet in tech, his interest keen, continues to inspire and convene.