Skip to content

Chapter 3 of 8

Control Flow and Functions

Programs make decisions with if, else if, and else blocks. The condition in an if statement is evaluated for truthiness, and JavaScript will coerce any non-boolean value into a boolean in that context. When many branches depend on the same expression, a switch statement provides a cleaner alternative: it evaluates the expression once and dispatches to the matching case, with break statements used to stop execution from falling through, and an optional default branch for any unmatched value.

Loops repeat actions. A for loop uses the form for (init; condition; increment) { ... }, where the initializer runs once, the condition is checked at the start of every iteration, and the increment runs at the end. The while loop also checks its condition before each iteration, but the do-while loop flips that order: it executes the body first and then checks the condition, guaranteeing at least one iteration. These looping constructs let you traverse arrays, repeat calculations, or process user input until a stop condition is reached.

Functions are reusable blocks of behavior. They can be declared with function name(parameters) { ... }, in which case the declaration is hoisted so it can be called from earlier in the file, or assigned as expressions such as const func = function() {}, which are not hoisted. Parameters are the named variables in the function definition, while arguments are the actual values passed when calling the function; the legacy arguments object exposes every passed value. The return statement exits the function and sends a value back to the caller, and a function with no return implicitly returns undefined. Strict mode, enabled by placing the string literal 'use strict'; at the top of a script or function, enforces safer rules such as disallowing undeclared variables and duplicate parameter names.

All chapters
  1. 1JavaScript Foundations
  2. 2Variables, Types, and Operators
  3. 3Control Flow and Functions
  4. 4Data Structures and ES6 Syntax
  5. 5Scope, Hoisting, and Closures
  6. 6Objects, Classes, and Built-in APIs
  7. 7Asynchronous JavaScript
  8. 8Browser Interaction and Best Practices

Drill it

Reading is not remembering. These come from the Javascript Fundamentals deck:

Q

What is JavaScript?

JavaScript is a high-level, interpreted programming language primarily used for web development to make web pages interactive. It is the scripting language of t...

Q

Who created JavaScript and in what year?

JavaScript was created by Brendan Eich in 1995 while working at Netscape. It was initially called Mocha, then LiveScript, before being renamed JavaScript.

Q

What is ECMAScript?

ECMAScript (ES) is the standardized scripting language specification upon which JavaScript is based. The latest stable version is ES2023, with JavaScript implem...

Q

How do you include JavaScript in an HTML file?

Use the <script> tag in the HTML, either inline with code between tags or externally via <script src="script.js"></script>. Place it before th...