[VL] Statements

Chapter Five: Statements in JavaScript

Introduction to Statements

  • Definition of Statements:

    • A statement in JavaScript can be understood as a command that executes an action.

    • It consists of expressions, and by combining expressions with statements, you create powerful programming constructs.

    • Comparison with English Language:

    • Expressions are like words (e.g., "jump", "catch", "fish"), which evaluate to something (values).

    • Statements are like sentences (e.g., "Go to the store", "Buy a car"), which carry out a command and end with a period (or in programming, a semicolon).

Characteristics of Statements

  • Key Features of Statements:

    • Statements are executed, whereas expressions are evaluated.

    • A program consists of a sequence of statements.

  • Semicolon Usage:

    • Statements generally end with a semicolon (though semicolons are optional). It is recommended to always use semicolons for clarity.

Examples of Expression Statements

  • Concatenation Example:

    • Example of an expression statement:

    • greeting = "Hello" + name;

      • Here, "Hello" + name is an expression that concatenates a string.

  • Increment Operation:

    • counter++;

    • This statement increments the value of the variable counter by 1.

  • Math Functions:

    • Example of an expression statement:

    • Math.cos(x);

      • This calculates the cosine of x.

Statement Blocks

  • Definition of Statement Blocks:

    • A statement block is a grouping of statements enclosed in curly braces {} that allows multiple statements to be treated as a single statement.

  • Structure of a Statement Block:

    • Example structure:

    • { x = Math.PI; cx = Math.cos(x); console.log(cx); }

    • Statements within a block are executed sequentially.

  • Semicolons in Blocks:

    • Statements inside a block end with semicolons, but the block itself does not require a semicolon.

  • Indentation for Readability:

    • Indentation inside blocks is not syntactically necessary, but it improves readability.

Variable Scope in JavaScript

  • Insight on Scope:

    • Scope pertains to the visibility and lifecycle of variables.

    • JavaScript does not have block scope, meaning variables declared within a block will be accessible outside of it.

  • Examples of Variable Visibility:

    • In traditional programming languages, variables declared inside a block would be inaccessible from outside. However, in JavaScript, variables x and cx remain accessible after the block.

Empty Statements

  • Definition of Empty Statements:

    • An empty statement is represented by a semicolon (;). It serves as a placeholder where a statement is syntactically required but no action is needed.

Declaration Statements

  • Types of Declaration Statements:

    • Declaration statements utilize keywords like var and function to create variables and functions (although functions will be covered later).

  • Example of Variable Declaration:

    • Example:

    • var x = 2;

    • var y = Math.cos(0.75);

      • This combines declaration with assignment.

Conditional Statements

  • Standard Conditional Statements:

    • Standard conditional statements involve expressions that must evaluate to true or false.

    • Syntax:

    • if (expression) { ... statements ... }

    • Example of If Statement:

    • if (username == null) { username = "John Doe"; }

  • Single Statement Execution:

    • If only one statement follows the if, it doesn’t require a block.

  • Negative Condition Example:

    • When checking an address condition, if (!address) verifies if address is falsy.

    • If true, it will execute the statements inside the block.

Truthy and Falsy Values

  • Truthiness in JavaScript:

    • Any string can be used as a boolean expression.

    • An empty string evaluates to false, while a non-empty string evaluates to true.

    • Example to check empty string:

    • if (!address) { ... }

Else Statements

  • If-Else Structure:

    • Compares the if expression and executes corresponding statements.

    • Example:

    • if (n == 1) { console.log("You have one message."); } else { console.log("You have " + n + " messages."); }

Loop Constructs

  • Loop Types Discussed:

    • While Loop: Executes as long as a condition is true.

    • Do-While Loop: Similar, but checks condition after the loop runs once.

    • For Loop: A more traditional way to iterate over a range.

  • Break and Continue Statements:

    • The break statement allows immediate exit from the loop.

    • The continue statement skips the rest of the current iteration and moves to the next iteration of the loop.

    • Example of a break:

      • if (i == 5) { break; }

    • Example of a continue:

      • if (someCondition) { continue; }

Conclusion

  • The chapter provides insight into JavaScript statements, variable scope, conditionals, loops, and how these constructs interact with the flow of a program.

  • Understanding these fundamentals is crucial for building logical structures in JavaScript programming.