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