Control Structures

We’ve talked about the individual parts that make up JavaScript: the numbers, strings, and Booleans that serve as nouns in JavaScript sentences. In this section, we talk about JavaScript sentences and paragraphs—about combining the parts into larger code structures.

Running Code Only If Something Is True

Sometimes we want to skip over code. For example, we probably don’t want to animate the scene if the game is over. In these cases, we use the if keyword.

 gameOver = ​true
 if​ (gameOver) console.log(​'Game Over!!!'​)
 // "Game Over!!!"

Simple if statements like this can go on a single line. When more than one thing is being done after an if, we need to wrap these things inside curly braces.

 gameOver = ​true
 if​ (gameOver) ...

Get 3D Game Programming for Kids, 2nd Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.