Core Features and Flow Control
JavaScript requires precise management of data and execution flow. The language offers specific primitives and composite types. Execution depends on rigid control statements.
Variables and Basic Data Types
JavaScript dynamically types variables. A variable is a named memory location. Declaration relies on var, let, or const. Modern architecture rejects var due to function-scoping vulnerabilities and hoisting unpredictability. let enforces block scoping. const enforces immutable bindings.
Primitive Types
Primitives are immutable values. They represent the lowest level of language implementation.
- Number: JavaScript uses a single number type. It implements the IEEE 754 standard for double-precision 64-bit floats. There is no distinct integer type.
- String: A sequence of 16-bit unsigned integer values representing UTF-16 code units. Strings are immutable. Modification creates a new string in memory.
- Boolean: Represents logical entity. Values are strictly
trueorfalse. - Undefined: A variable that is declared but not initialized holds the
undefinedvalue. - Null: An intentional absence of any object value. It is a primitive, but
typeof nullincorrectly evaluates to"object"due to a legacy design flaw.
Composite Types
Composite types aggregate multiple values or complex entities. They are passed by reference, not by value.
- Object: A collection of key-value pairs. Keys are strings or symbols; values are any type.
- Array: An ordered list of values. Arrays are specialized objects with numerical indices and an automatic
lengthproperty.
Flow Control Statements
Flow control dictates the execution sequence. Without control structures, logic executes strictly top-to-bottom. Control structures introduce branching and repetition.
Conditional Branching
Conditional statements fork execution paths based on boolean evaluations.
The if Statement
The if statement evaluates an expression. If true, the block executes. If false, execution bypasses the block or falls to an else clause.
if (sensorValue > threshold) {
triggerAlarm();
} else {
maintainState();
}
The switch Statement
The switch statement evaluates a single expression against multiple case clauses. It uses strict equality (===). It requires explicit break statements to prevent fall-through execution. Fall-through is rarely intentional and causes critical logic failures.
Iteration and Loops
Loops execute a block of code repeatedly until a specific condition evaluates to false.
The while Loop
The while loop evaluates its condition before execution. If the condition is false initially, the block never executes.
The do-while Loop
The do-while loop executes the block first, then evaluates the condition. It guarantees at least one execution pass. This is necessary when initialization logic resides inside the loop body.
The for Loop
The for loop consolidates initialization, condition evaluation, and final-expression incrementation into a single line. It is the standard iteration structure for arrays or known integer ranges.
for (let i = 0; i < limit; i++) {
processItem(items[i]);
}
Input and Output Mechanics
JavaScript operating in a browser relies on the host environment for I/O operations. The core language lacks built-in I/O capabilities.
- Console API:
console.log()provides output directly to the developer console. It is an essential diagnostic tool. - Dialogs:
alert(),prompt(), andconfirm()provide rudimentary, blocking UI interactions. They halt thread execution until the user responds. Modern applications avoid them. - DOM Manipulation: True output modifies the Document Object Model. JavaScript reads user input from form fields and writes output by altering element properties like
innerHTMLortextContent.
Execution flow and data management form the operational foundation of JavaScript. Mastery of these features is non-negotiable.