JavaScript Syntax
JavaScript syntax is the collection of rules that define how you write correct, well-formed programs.
What Syntax Means
Syntax is simply the grammar of a programming language: the order and form in which words, symbols, and punctuation must appear so the engine can understand them. Just as a sentence needs correct grammar to make sense, a line of JavaScript needs correct syntax to run. Once you internalize a few core rules, most of the language starts to feel predictable.
Values, Variables, and Keywords
JavaScript works with two kinds of values: fixed values called literals (such as numbers and text) and variable values that you store under a name. You create variables using the keywords let and const, and you can perform operations on them with operators like the plus sign for addition or the equals sign for assignment.
Example
<!DOCTYPE html>
<html>
<body>
<h2>Values, Variables, and Keywords</h2>
<script>
// Literals: fixed values written directly in code
42;
"a piece of text";
// Variables hold values you can reuse
let score = 10;
const pi = 3.14159;
// Operators combine values
let doubled = score * 2;
</script>
</body>
</html>- Use const for values that should never be reassigned.
- Use let when a value needs to change later.
- Names are case-sensitive: total and Total are two different variables.
- Text values must be wrapped in single or double quotes.
Identifiers and Naming Rules
An identifier is the name you give to a variable, function, or other item. Identifiers may contain letters, digits, underscores, and dollar signs, but they cannot begin with a digit. By convention, most JavaScript names use camelCase, where the first word is lowercase and each following word starts with a capital letter.
Example
<!DOCTYPE html>
<html>
<body>
<h2>Identifiers and Naming Rules</h2>
<script>
// Valid identifiers
let firstName = "Ada";
let _count = 0;
let $element = document.body;
let userAge2 = 30;
// Invalid: cannot start with a digit
// let 2ndPlace = "silver";
</script>
</body>
</html>Exercise: JavaScript Syntax
Are JavaScript variable names case-sensitive?