bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths
Learn/JavaScript/JavaScript Foundations
JavaScript•JavaScript Foundations

JavaScript Control Flow

Control Flow is the order in which statements are executed in a program.

By default, JavaScript runs code from top to bottom and left to right.

Control flow statements let you change that order , based on conditions, loops or keywords.

Default Flow

Default flow executes code sequentially (from top to bottom / from left to right).

Example

let x = 5;
let y = 6;
let z = x + y;

Conditional Control Flow

Conditions let you make decisions using

  • if
  • if...else
  • switch
  • ternary (? :)

Example

let text = "Unknown";
if (age >= 18) {
  text = "Adult";
} else {
text = "Minor";
}

Loops (Repetition Control Flow)

  • for
  • while
  • do...while

Example

for (let i = 0; i < 5; i++) {
  text += "The number is " + i + "<br>";
}

Jump Statements

Jump statements let you change the flow abruptly using:

  • break - exits a loop or switch
  • continue - skips the current loop iteration
  • return - exits from a function
  • throw - jumps to error handling

Example

for (let i = 0; i < 10; i++) {
  if (i === 3) { break; }
  text += "The number is " + i + "<br>";
}

Function Flow

Functions are callable and reusable code blocks

Functions will run when they are called.

Example

function myFunction(p1, p2) {
  return p1 * p2;
}

JavaScript Is Single-Threaded

JavaScript runs on a single thread .

It can only do one thing at a time .

Every task has to wait for the previous one to finish.

This can freeze an application during slow operations (like file requests).

JavaScript can also run code asynchronously.

Asynchronous programming is covered in the JS Advanced section:

JavaScript Asynchronous Programming .

Previous

JavaScript Logical Operators

Next

JavaScript Function Arguments

This chapter

Overview
51

Lessons

194m

Read time

1. JavaScript Tutorial2. JavaScript Syntax3. JavaScript Operators4. JavaScript if5. JavaScript Loops6. JavaScript Functions7. JavaScript Scope8. JavaScript Data Types9. JavaScript Style Guide10. JavaScript Introduction11. JavaScript Statements12. JavaScript Arithmetic13. JavaScript else14. JavaScript For Loop15. JavaScript Functions16. JavaScript Code Blocks17. JavaScript Primitives18. JavaScript Best Practices19. JavaScript Where To20. JavaScript Comments21. JavaScript Assignment22. The JavaScript Ternary Operator23. JavaScript While Loops24. Invoking JavaScript Functions25. JavaScript Hoisting26. JavaScript Built-In Objects27. JavaScript Common Mistakes28. JavaScript Output29. JavaScript Variables30. JavaScript Comparison31. JavaScript Switch Statement32. JavaScript Break33. JavaScript Function Parameters34. JavaScript Use Strict35. JavaScript typeof36. JavaScript Performance37. JavaScript Let38. JavaScript Conditionals39. JavaScript Booleans40. JavaScript Continue41. JavaScript Function Return42. JavaScript toString()43. JavaScript Const44. JavaScript Logical Operators45. JavaScript Control Flow46. JavaScript Function Arguments47. JavaScript Type Conversion48. JavaScript Datatypes49. JavaScript Function Expressions50. JavaScript Destructuring51. JavaScript Arrow Functions

On this page

Default FlowConditional Control FlowLoops (Repetition Control Flow)Jump StatementsFunction FlowJavaScript Is Single-Threaded