JavaScript Syntax
JavaScript syntax defines the rules for writing valid code: how values, operators, expressions, keywords and comments fit together.
JavaScript is case-sensitive, meaning `myVar` and `myvar` are different identifiers. It largely ignores extra whitespace, letting you format code for readability.
Values and identifiers
Literal values like numbers and strings are called values. Identifiers are names given to variables and functions; they must start with a letter, underscore, or dollar sign.
Keywords
Reserved words like `let`, `const`, `function`, `if`, `return` have special meaning and cannot be used as identifiers.
let price = 19.99;
let productName = "Book";
console.log(productName, price);Book 19.99Two variable declarations followed by a console output.
let $el = "special";
let _private = "hidden";
console.log($el, _private);special hiddenIdentifiers may start with $ or underscore.
Key points
- JavaScript is case-sensitive.
- Identifiers can include letters, digits, $ and _ but not start with a digit.
- Reserved keywords cannot be used as variable names.
- Extra whitespace and line breaks are generally ignored.
