iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

JS Syntax

JavaScript syntax is the set of rules that defines a valid program. Most of it should feel familiar if you've seen any C-family language.

The core rules

  • Case-sensitive — total and Total are different.
  • Whitespace and line breaks are ignored (mostly — see ASI).
  • Statements end with ; (auto-inserted in most cases).
  • Blocks are wrapped in { }.
  • Strings are "…", '…', or `…`.
  • Comments are // … or /* … */.

Identifiers (names)

RuleOKNot OK
Must start with letter, $, or _name, $x, _tmp2nd, -name
Letters, digits, $, _ onlyuser1user-name
Not a reserved wordclsclass, return

Tiny program

JS
// declarations
const name = "Ada";
let   xp   = 0;

// function declaration
function gain(amount) {
  xp += amount;
  return xp;
}

// control flow
if (gain(50) >= 50) {
  console.log(`${name} levelled up!`);
}
Tip: Read JavaScript style guides (Airbnb, Standard) once. Most teams pick one, lint against it, and never argue about syntax again.

Example

Example
<!DOCTYPE html>
<html>
<body>

<p id="out"></p>

<script>
document.getElementById("out").textContent = "Hello from JS Syntax!";
</script>

</body>
</html>
Try it Yourself »

Exercise

Pick a valid identifier name to declare a counter.

let = 0;

Test yourself

Q1. JavaScript identifiers can start with…
Q2. JavaScript is…
Q3. Block comments use…

Discussion

Loading…