Operators, Expressions, and Core Statements
JavaScript combines values into expressions. Operators process these values. Statements form the operational logic that executes tasks. The language demands explicit termination and strict handling of whitespace.
Statement Basics: Execution and Termination
Statements are commands. They dictate execution. JavaScript is forgiving with whitespace. Spaces, tabs, and newlines separate tokens but do not affect execution logic.
Termination defines the end of a statement. The semicolon (;) is the explicit terminator. JavaScript implements Automatic Semicolon Insertion (ASI). The engine attempts to infer missing semicolons during parsing. ASI is a structural hazard. It causes unpredictable execution when returning objects or anonymous functions. Robust code mandates explicit semicolons to prevent parser misinterpretations.
Blocks aggregate multiple statements into a single executable unit. Curly braces {} delineate a block. Control structures use blocks to execute multi-line logic conditionally.
Operators: Processing Data
Operators mutate or evaluate operands. JavaScript enforces a strict precedence hierarchy.
- Assignment Operator (
=): Assigns the right operand’s value to the left operand. Compound assignments (+=,-=) combine arithmetic and assignment operations. - Arithmetic Operators: Perform mathematical calculations. They include addition (
+), subtraction (-), multiplication (*), division (/), and modulus (%). The modulus operator yields the remainder of a division. It is critical for cyclical algorithms. - Increment/Decrement Operators: Modify a numeric variable by one. Prefix application (
++x) modifies the value before evaluation. Postfix application (x++) evaluates the current value before modification. - Logical Operators: Evaluate boolean conditions. AND (
&&) evaluates true only if both operands are true. OR (||) evaluates true if either operand is true. NOT (!) inverts the boolean value. Short-circuit evaluation halts processing as soon as the result is determined. - Bitwise Operators: Operate on 32-bit binary representations of numbers. They include AND (
&), OR (|), XOR (^), and NOT (~). - Bitwise Shift Operators: Move binary bits left (
<<) or right (>>,>>>). They execute rapid mathematical operations at the hardware level. The zero-fill right shift (>>>) pushes zeros in from the left, discarding the sign bit. - The
voidOperator: Evaluates an expression and explicitly returnsundefined. Historically used to prevent browser navigation when clicking anchor links. - Object Operators: Manipulate composite types. The
deleteoperator removes a property from an object. Theinoperator checks if an object possesses a specific property. Theinstanceofoperator verifies the prototype chain.
Core JavaScript Statements
Statements direct execution flow through loops and conditional branches.
The if and switch Statements
The if statement executes a block if its evaluated expression is truthy.
The switch statement evaluates an expression and routes execution to matching case clauses. It replaces nested if-else chains. Execution requires an explicit break statement to exit the block. Without a break, the engine continues executing subsequent cases.
Looping Mechanisms
Loops automate repetitive execution.
whileLoop: Evaluates the condition before execution. High-risk for infinite loops if the condition lacks an exit trigger.do-whileLoop: Executes the block before evaluating the condition. Ensures at least one execution cycle.forLoop: Consolidates initialization, condition, and increment logic. Designed for known iteration counts.
Object-Related Statements
Iteration over composite types requires specialized statements.
The for...in Loop
The for...in loop iterates over the enumerable properties of an object. It traverses the prototype chain.
for (let key in sensorData) {
console.log(key, sensorData[key]);
}
It extracts the keys as strings. It does not guarantee iteration order. It is unsuitable for arrays where numerical order matters. Modern architecture prefers Object.keys() or the for...of loop for array iteration.