JavaScript Syntax

JavaScript syntax is the set of rules that define how JavaScript programs are constructed, structured, and interpreted by the JavaScript engine. Understanding these fundamental syntax rules is essential for writing code that runs correctly and follows established conventions.

JavaScript Syntax Rules

JavaScript syntax refers to the set of rules that define how JavaScript programs are written and understood by the JavaScript engine. Understanding syntax is essential—even small syntax errors will prevent your code from running. The browser's JavaScript engine is strict about these rules and will throw errors when they're violated.

JavaScript statements are composed of various elements: values (literals and variables), operators (+, -, *, etc.), expressions (combinations of values and operators), keywords (reserved words like var, if, function), and comments (notes for developers). These elements combine according to specific rules to form complete instructions that the JavaScript engine can execute.

Statements are typically separated by semicolons (;), though JavaScript has a feature called automatic semicolon insertion that adds them where it thinks they belong. However, relying on automatic insertion can lead to subtle, hard-to-find bugs in certain situations. It's considered best practice to explicitly include semicolons at the end of statements for clarity and to prevent potential issues.

JavaScript is case-sensitive, meaning myVariable, myvariable, MyVariable, and MYVARIABLE are four completely different identifiers. This case sensitivity extends to all keywords, function names, variable names, and identifiers throughout the language. Typing console.Log() instead of console.log() will cause an error because Log is not the same as log. Always be consistent and precise with your capitalization.

The JavaScript convention is to use camelCase naming for variables and functions: start with a lowercase letter and capitalize the first letter of subsequent words (like firstName, calculateTotalPrice, isUserLoggedIn). Constructor functions and classes use PascalCase (like UserAccount, ShoppingCart). Constants often use UPPER_SNAKE_CASE (like MAX_SIZE, API_KEY). Following these conventions makes your code more readable and familiar to other JavaScript developers.

JavaScript ignores extra whitespace (spaces, tabs, and line breaks), allowing you to format code for readability without affecting how it runs. You can add spaces around operators (x = 5 + 3), indent code blocks for structure, and break long statements across multiple lines. Good formatting makes code significantly easier to read and maintain, even though it doesn't change the functionality.

// JavaScript statements
var x = 5;
var y = 6;
var z = x + y;

// Multiple statements on one line
var a = 1; var b = 2; var c = 3;

// Case sensitivity
var myVar = "Hello";
var myvar = "World"; // Different variable!

Values and Literals

JavaScript recognizes two types of values: fixed values (called literals) that you write directly in your code, and variable values that can change during program execution. Literals are constant values that represent themselves—the number 42 is always 42, the string "hello" is always that specific text.

Number literals are numeric values written with or without decimal points (like 10, 3.14, or 1000). JavaScript doesn't distinguish between integers and floating-point numbers—all numbers are stored internally as 64-bit floating-point values following the IEEE 754 standard. You can write numbers in decimal notation (10), hexadecimal with 0x prefix (0xFF), octal with 0o prefix (0o77), or binary with 0b prefix (0b1010).

String literals are sequences of text characters written within either single quotes ('text'), double quotes ("text"), or backticks (`text`). Single and double quotes are functionally identical—choose one style and use it consistently throughout your code. Backticks create template literals that support multi-line strings and embedded expressions using ${} syntax, making them more powerful for dynamic string construction.

Expressions are combinations of values, variables, and operators that JavaScript evaluates to produce a result. For example, 5 * 10 is an expression that evaluates to 50. Expressions can be simple (like a single variable name) or complex (like calculations involving multiple operations and function calls). Every expression produces a value that can be used in other expressions or assigned to variables.

Keywords are reserved words that have special meaning in JavaScript and perform specific actions. Keywords like var, let, and const declare variables, if and else control program flow, function defines functions, return exits functions with a value, and for and while create loops. You cannot use keywords as variable names or identifiers because they're reserved for the language's syntax structure.

// Number literals
var x = 10.50;
var y = 1001;

// String literals
var name = "John Doe";
var greeting = 'Hello World';

// Expressions
var result = 5 * 10;

// Keywords
var message = "Hello";
if (x > 0) {
  console.log(message);
}