← Back to JavaScript Course | Chapter 1: Basics & Syntax | Lesson 5 of 12

JS Syntax

Every language, spoken or programming, has grammar rules that make it understandable, subjects, verbs, and punctuation all have to line up in a way both sides agree on. JavaScript syntax is that grammar, it defines how values, operators, expressions, and keywords fit together into valid instructions the browser can actually run. Get the grammar wrong, and the engine simply cannot understand what you meant. Once you're comfortable with JavaScript's basic building blocks, values, operators, and identifiers, reading any script, including the ones powering the tutorials on cookiescursor.com, becomes far less intimidating.

Values

A value is a single piece of data that JavaScript can work with, such as a number, a piece of text, or true and false, and values are the raw material every JavaScript program manipulates.

Note: There are two kinds of values in JavaScript, literal values you type directly like 5 or hello, and variable values stored under a name.

Warning: Text values must always be wrapped in quotes, forgetting quotes around text is one of the most common beginner syntax errors.

Example: Values

javascript
console.log(5);
console.log("hello");
console.log(true);

Operators

Operators are symbols that perform actions on values, such as + to add numbers or join text, and = to assign a value to a variable, forming the connective tissue between values in an expression.

Note: The same + operator behaves differently depending on the values around it, adding numbers together but joining text side by side.

Warning: Mixing a number and a string with + can produce surprising results, since JavaScript often converts the number into text rather than adding it.

Example: Operators

javascript
console.log(2 + 3);
console.log("Hello" + " World");

Expressions

An expression is any piece of code that produces a value, ranging from a simple literal to a complex combination of operators and function calls, and expressions are the pieces statements are built from.

Note: You can always test whether something is a valid expression by asking, does this produce a value I could store or log?

Warning: Not every valid piece of syntax is an expression, a full statement like an if block does not itself produce a usable value.

Example: Expressions

javascript
let result = 2 + 3 * 4;
console.log(result);

Keywords and Identifiers

Keywords are reserved words with special meaning in JavaScript, like const, function, and if, that you cannot use as your own names, while identifiers are the custom names you choose for variables, functions, and other pieces of your code.

Note: Choose descriptive identifier names, like userEmail instead of x, future readers of your code, including yourself, will appreciate it.

Warning: Attempting to use a reserved keyword as an identifier, such as naming a variable const, immediately causes a syntax error.

Example: Keywords and Identifiers

javascript
const userEmail = "[email protected]";
console.log(userEmail);

Case Sensitivity and camelCase

JavaScript is fully case sensitive, meaning userName and username are treated as two entirely different identifiers, and the community convention for naming variables and functions is camelCase, where the first word is lowercase and each following word starts with a capital letter.

Note: Stay consistent with camelCase across an entire project, mixing naming styles makes code harder to scan and search.

Warning: A typo that only differs in letter case, like calling getUserData() when the function is actually named getUserdata(), causes a reference error rather than a helpful warning.

Example: Case Sensitivity and camelCase

javascript
let userName = "Alice";
let username = "Bob";
console.log(userName, username);
Common Mistakes
  1. Using a reserved keyword, like class or return, as a variable name, which causes an immediate syntax error.
  2. Mixing up case sensitivity, JavaScript treats myVariable and myvariable as two completely different identifiers.
  3. Starting an identifier with a number, valid identifiers must begin with a letter, underscore, or dollar sign.
Chapter Summary
  • Values, operators, expressions, and keywords are the fundamental building blocks of JavaScript syntax.
  • Identifiers are the names given to variables, functions, and other entities, and they follow specific naming rules.
  • JavaScript is case sensitive, and camelCase is the standard naming convention for variables and functions.
Browser Support

Core JavaScript syntax rules, values, operators, expressions, and identifier rules, are part of the language standard and behave identically in every browser.

🔒

Chapter Quiz — Complete all 12 topics to unlock

0/12 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.