JavaScript · Lesson 03 / 32 · English
JavaScript Syntax
Understand the idea. Change the code. Build confidence with one focused experiment.
Read this lesson in தமிழ் ↗.
Start with the idea
Statements describe actions. Strings need matching quotes, identifiers are case-sensitive, and braces group statements into a block.
Learning goal: Explain what the example does, predict its result, and change it to solve the practice task.
Step-by-step lesson notes
JavaScript Syntax Basics
JavaScript syntax is the set of rules that define how a JavaScript program is written and interpreted. Understanding the basics prevents hard-to-find bugs.
Statements and Semicolons
JavaScript programs are made up of statements. A semicolon marks the end of a statement. JavaScript has Automatic Semicolon Insertion (ASI) — but relying on it can cause bugs.
// Each statement should end with ;
let x = 5;
let y = 10;
let sum = x + y;
// Multiple statements per line (avoid — hard to read)
let a = 1; let b = 2; let c = 3;
// ASI gotcha — never start a line with ( or [
const fn = () => console.log("hi")
(function() { /* this gets called as fn()! */ })()Case Sensitivity
// JavaScript is CASE-SENSITIVE
let name = "Alice";
let Name = "Bob"; // completely different variable!
let NAME = "Charlie"; // yet another variable!
// Keywords must be lowercase
// Let x = 5; // ❌ SyntaxError
let x = 5; // ✅Whitespace and Indentation
// JavaScript ignores extra whitespace
let x=5; // works
let y = 5; // works (readable!)
let z = 5; // works
// Use 2 or 4 spaces for indentation (be consistent)
if (x > 0) {
console.log("positive");
if (x > 10) {
console.log("large");
}
}Identifiers
An identifier is a name for a variable, function, or class.
| Convention | Usage | Example |
|---|---|---|
| camelCase | Variables, functions | userName, getTotal() |
| PascalCase | Classes, constructors | UserAccount, ShoppingCart |
| UPPER_SNAKE_CASE | Constants | MAX_SIZE, API_URL |
| _prefix | Private (convention) | _internalState |
| $prefix | DOM elements, libraries | $element, jQuery |
Tip: Use descriptive names. getUserById is better than g or func1.
A complete example
Run this complete index.js program in the JavaScript console editor below. The earlier reference fragments may need a browser page or other surrounding setup.
const course = "JavaScript";
if (course === "JavaScript") {
console.log("Ready to learn");
}Predict first. Reveal the expected result.
Ready to learn
Make one change. See what happens.
Load the example, change a value, predict the output, and run it again. Use the console output to check your reasoning.
Wait for the editor to load, then load this example and press Run.
Powered by the free OneCompiler editor. Code you run is sent to that external service. Keep personal data and secret keys out of your examples.
Put the idea to work
Practice challenge
Change course to "Java". Will the message appear?
Reveal the answer and reasoning
No. The strict equality check is false, so the block is skipped.
One mistake to avoid: console.log and console.Log are different property names. A missing closing quote or brace is a syntax error.
Your learning checkpoint
- Explain the example in your own words.
- Complete the challenge before revealing its answer.
- Describe the common mistake and how to avoid it.