Description and basic syntax

Description and Basic Syntax

JavaScript is the programming language of the web, transforming static HTML documents into dynamic, interactive applications. Along with HTML (structure) and CSS (presentation), JavaScript provides the behavior layer that brings web pages to life.

Core Characteristics:

  • High-level Language: Abstracts away memory management and low-level details

  • Just-in-Time Compiled: Modern engines compile JavaScript for performance

  • Dynamic Typing: Variable types are determined at runtime

  • Single-threaded: Executes one operation at a time

  • Non-blocking & Asynchronous: Can handle multiple operations through callbacks and promises

  • Multi-paradigm: Supports procedural, object-oriented, and functional programming styles

JavaScript in 100 Seconds

The evolution of JavaScript

JavaScript was created in 1995 by Brendan Eich at Netscape to add interactivity to web pages. Since then, it has evolved dramatically:

  • ECMAScript: The standardized specification that JavaScript implements

  • ES6 (2015): Revolutionary update that modernized the language (covered in Advanced JavaScript)

  • Modern JavaScript: Continuous yearly updates adding powerful features

Today, JavaScript runs not just in browsers but also on servers (Node.js), mobile devices, desktop applications, and even embedded systems.

Adding JavaScript to a web page

JavaScript can be integrated into HTML in three ways, each with specific use cases:

1. Inline JavaScript (avoid in production)

2. Internal JavaScript (script tags)

JavaScript syntax fundamentals

Statements and Comments

JavaScript code consists of statements that perform actions. Each statement typically ends with a semicolon (optional but recommended).

Case sensitivity and naming conventions

JavaScript uses camelCase for variable and function names (myVariable, getUserData), UPPER_SNAKE_CASE for constants (MAX_VALUE), and PascalCase for class names (UserAccount). Following these conventions makes code readable and helps other developers understand your intent. The language is case-sensitive, so variable, Variable, and VARIABLE are three distinct identifiers.

Variables and data types

Variables are containers that store data values. JavaScript has three ways to declare variables, each with different behaviors:

Variable Declaration Keywords

JavaScript Data Types

JavaScript has eight data types: seven primitive types and objects.

JavaScript Data Types

Primitive Data Types

Object data type

Objects store collections of data as key-value pairs. Properties can be accessed using dot notation (person.firstName) or bracket notation (person["firstName"]). Bracket notation is required when property names have spaces, special characters, or are stored in variables. Arrays are special objects optimized for ordered data, accessed by numeric indices starting at 0. Arrays can contain mixed types, and their length property always reflects the number of elements.

Type checking and conversion

Functions

Functions are reusable blocks of code that perform specific tasks. They are fundamental building blocks in JavaScript and are themselves values that can be passed around.

Function declaration

Function Scope

JavaScript uses lexical (static) scoping where variables are accessible based on where they're defined in the code. Variables declared outside functions are global and accessible everywhere. Variables declared inside functions are local and only accessible within that function. Modern let and const use block scope (within {}), while old var uses function scope, which is another reason to avoid var. Function parameters and locally declared variables are not accessible outside the function, preventing naming conflicts and improving code organization.

Function expressions and arrow Functions

Operators

JavaScript provides various operators for performing operations on values and variables.

JavaScript Operators

Arithmetic operators

Arithmetic operators perform mathematical calculations. The modulus operator (%) returns the remainder after division, useful for checking even/odd numbers or cycling through values. Exponentiation (**) raises a number to a power. Increment (++) and decrement (--) operators have two forms: postfix (x++) assigns the current value then increments, while prefix (++x) increments then assigns the new value.

Assignment operators

Compound assignment operators combine an operation with assignment, providing shorter syntax. x += 5 is equivalent to x = x + 5. These operators work with all arithmetic operations and are more concise and often more performant than writing out the full expression. The += operator is particularly useful for string concatenation and building messages incrementally.

Comparison operators

Logical operators

Logical operators work with boolean values and use short-circuit evaluation. The AND operator (&&) returns the first falsy value or the last value if all are truthy.

The OR operator (||) returns the first truthy value or the last value if all are falsy. This behavior enables common patterns like default values (value || defaultValue) and conditional execution (condition && executeThis()).

The NOT operator (!) converts values to boolean and inverts them—double NOT (!!) is a trick to convert any value to its boolean equivalent.

Ternary (Conditional) Operator

Conditional statements

Conditional statements control program flow based on boolean conditions.

If / Else If / Else

Conditional statements execute different code blocks based on boolean conditions. The if statement runs code only if its condition is true. The else clause handles the false case. The else if allows multiple conditions to be checked in sequence—only the first true condition executes. Conditions can use logical operators to combine multiple checks. Avoid deeply nested conditionals when possible by using early returns or restructuring logic for better readability.

Switch statements

Switch statements provide cleaner syntax than multiple if-else statements when checking a single value against many discrete options. Each case clause checks for equality (strict ===). The break statement prevents fall-through to subsequent cases—without it, execution continues to the next case. Fall-through can be intentional (grouping multiple cases) but should be clearly commented. The default clause acts like else, handling values that don't match any case.

Looping statements

Loops execute code repeatedly, either a specific number of times or until a condition is met.

For loop

The for loop is ideal when you know how many iterations you need. It has three parts: initialization (runs once before the loop), condition (checked before each iteration), and increment (runs after each iteration). The loop variable (typically i) is commonly used to access array elements by index. The break statement exits the loop immediately, while continue skips the rest of the current iteration and moves to the next one. Nested loops multiply iteration counts—a loop inside a loop with 10 iterations each runs 100 times total.

For...of and for...in loops

While and do...while loops

While loops repeat code as long as a condition is true, making them ideal when the number of iterations is unknown. The while loop checks the condition before executing, so the body may never run. The do...while loop checks the condition after executing, guaranteeing at least one execution. Use while for scenarios like processing input until a condition is met, searching for values, or waiting for external events. Always ensure the loop condition will eventually become false to avoid infinite loops.

Avoiding Infinite Loops

Best practices

1. Use modern variable declarations

2. Write clear function names

3. Keep functions small and focused

4. Use strict equality

5. Comment complex logic, not obvious code

Good JavaScript code is readable, maintainable, and follows established conventions. Use const by default and let only when reassignment is necessary—never use var. Write descriptive function and variable names that explain purpose. Keep functions small and focused on a single task. Always use strict equality (===) to avoid type coercion bugs. Comment complex logic, algorithms, or business rules, but don't comment obvious code—good code should be self-documenting through clear naming.

Last updated

Was this helpful?