Lesson 1: JavaScript Syntax Overview

Learning Objectives

By the end of this lesson, learners will:

  • Understand what JavaScript syntax is and why it matters
  • Be able to write basic JavaScript statements correctly
  • Learn about semicolons, line breaks, case sensitivity, indentation, and comments
  • Gain confidence in writing and organizing their first JavaScript code snippets

What Is Syntax in Programming?

Syntax is like grammar for a programming language.
Itโ€™s a set of rules and structure that determines how code must be written so the computer can understand and execute it.

Just like a sentence in English has a subject and verb, JavaScript statements must follow certain rules.


The Building Blocks of JavaScript Syntax


1. Statements

A statement is a single instruction in JavaScript โ€” like a sentence in English.

๐Ÿ“Œ Example:

let message = "Hello, Codevisionz!";

Here, weโ€™re telling JavaScript to:

  • Create a variable named message
  • Assign the string "Hello, Codevisionz!" to it

2. Semicolons (;)

Semicolons are used to end a statement in JavaScript.

๐Ÿ“Œ Example:

let x = 5;
let y = 10;
console.log(x + y);

๐Ÿ”น JavaScript usually allows omitting semicolons, but itโ€™s good practice to use them to avoid unexpected behavior.


3. Case Sensitivity

JavaScript is case-sensitive โ€” this means:

let name = "Alice";
let Name = "Bob";

These are two different variables!

๐Ÿ“Œ Best Practice: Stick to camelCase for variables (like userName, totalAmount).


4. Curly Braces {} for Code Blocks

Curly braces are used to group multiple statements into one block โ€” like in functions, loops, or conditionals.

๐Ÿ“Œ Example:

if (true) {
  console.log("This will run!");
}

Everything inside {} is treated as a single block of code.


5. Indentation and Formatting

While not required by JavaScript, indentation (spacing code to show structure) is critical for readability.

๐Ÿ“Œ Example:

if (score > 50) {
  console.log("You passed!");
} else {
  console.log("Try again.");
}

Use 2 or 4 spaces consistently to indent blocks.


6. Comments

Comments let you explain your code. JavaScript ignores comments when running the code.

Single-line comment:

// This is a comment

Multi-line comment:

/*
  This is a
  multi-line comment
*/

โœ… Use comments to:

  • Explain complex logic
  • Describe what your code is doing
  • Temporarily disable code (for testing)

Practice Exercise (Try in Browser Console)

// This is a simple calculation
let a = 5;
let b = 3;
let result = a + b;

console.log("The result is: " + result); // Output: The result is: 8

Common Beginner Mistakes

MistakeWhy Itโ€™s WrongFix
Let x = 5;Incorrect capitalization (Let is not recognized)Use let
console.log("Hello')Missing closing quoteUse matching quotes: "Hello"
if x > 5 console.log("Yes");Missing parentheses {} and ()Correct: if (x > 5) { console.log("Yes"); }

Summary Table

ConceptExplanation
StatementA line of code that does something
SemicolonEnds a statement (;)
Curly Braces {}Group multiple statements
Case-Sensitiveusername โ‰  UserName
CommentNotes inside code ignored by the computer
IndentationSpacing to improve readability