Skip to content

Latest commit

 

History

History
60 lines (42 loc) · 1.49 KB

6dv1.md

File metadata and controls

60 lines (42 loc) · 1.49 KB

lang.js.syntax.statements

A JavaScript program is a sequence of statements. Each statement is an instruction for the computer to do something

Synopsis

  if (/* expression slot */) {
    // More statements here
  }

Overview

A JavaScript program consists of a sequence of statements. Each statement is an instruction to do something, like create a variable, run an if/else condition, or start a loop

Statements are the rigid structure that holds our program together, together with expressions they build the basics of writting js programs

Statements often have slots for expressions. We can put any expression we like into those slots.

Cookbook

Many types of expression with a single statement

A statement has an otherwise empty expresion slot that can be filled with any kind of expression

  /* 
  For the following expresion:

  let hi = {{some expression}};
  */

  let hi = 1;
  let hi = "hello";
  let hi = 5 * 10;
  let hi = num > 100;
  let hi = isHappy ? "🙂" : "🙁";
  let hi = [1, 2, 3].pop();

How to know if chunk of js is an statement

Want to know whether a chunk of JS is an expression or a statement? Try to log it out!

If it runs, the code is an expression. If you get an error, it's a statement (or, possibly, invalid JS). With this we can also see what the expression resolves to, as it will be printed out

  console.log(/* Some chunk of JS here */);