DEV Community

Karthick Narayanan
Karthick Narayanan

Posted on

JavaScript Part 1: Syntax, Statements, Comments & Variables

1. JavaScript Syntax

Syntax is simply the set of rules that define how JavaScript code must be written just like grammar rules in English.

JavaScript is case-sensitive. This means name, Name, and NAME are all treated as different things.

let name = "Karthick";   // ✅ valid
let Name = "Ravi";       // also valid, but different variable
Enter fullscreen mode Exit fullscreen mode

Whitespace (spaces, tabs, blank lines) is mostly ignored by JavaScript, but using it properly makes your code readable.

// Hard to read
let x=5;let y=10;let z=x+y;

// Easy to read
let x = 5;
let y = 10;
let z = x + y;
Enter fullscreen mode Exit fullscreen mode

2. JavaScript Statements

A statement is a single instruction that tells JavaScript to do something. Think of it like a sentence in English — it expresses a complete action.

let age = 25;             // declares a variable
console.log(age);         // prints the value to the console
alert("Hello, World!");   // shows a popup in the browser
Enter fullscreen mode Exit fullscreen mode

Semicolons

Statements usually end with a semicolon ;. While JavaScript can often figure out where a statement ends without it (a feature called ASI — Automatic Semicolon Insertion), it's good practice to always include them.

let city = "Chennai";   // ✅ with semicolon — recommended
let city = "Chennai"    // also works, but avoid relying on this
Enter fullscreen mode Exit fullscreen mode

Code Blocks

Multiple statements can be grouped inside curly braces {}. You'll see this a lot with functions and conditions.

if (age > 18) {
  console.log("Adult");
  console.log("Can vote");
}
Enter fullscreen mode Exit fullscreen mode

3. JavaScript Comments

Comments are notes you write in your code for yourself or other developers. JavaScript completely ignores them when running the code.

There are two types:

Single-line comments

Use // to comment out a single line.

// This is a single-line comment
let price = 500; // price is in rupees
Enter fullscreen mode Exit fullscreen mode

Multi-line comments

Use /* ... */ to comment across multiple lines.

/*
  This function calculates the total price
  including GST.
  Written by: Karthick
*/
function calculateTotal(price) {
  return price * 1.18;
}
Enter fullscreen mode Exit fullscreen mode

💡 Tip: Use comments to explain why you did something, not just what you did. The code itself shows what — the comment should explain why.


4. JavaScript Variables

A variable is like a labelled box — it stores a value that your program can use later.

Declaring a variable with var

var is the old way to declare variables (before 2015). You'll still see it in older code.

var greeting = "Hello!";
console.log(greeting); // Hello!
Enter fullscreen mode Exit fullscreen mode

var has some quirky behavior (like being accessible outside the block it was defined in), which is why modern JavaScript uses let and const instead — covered in Part 2!

Naming variables

Variable names in JavaScript must follow these rules:

  • Must start with a letter, underscore _, or dollar sign $
  • Cannot start with a number
  • Cannot use reserved keywords like let, return, function
  • Are case-sensitive
let firstName = "Priya";    // ✅ valid
let _count = 0;             // ✅ valid
let $total = 100;           // ✅ valid
let 1stName = "Ravi";       // ❌ invalid — starts with a number
let let = "something";      // ❌ invalid — reserved keyword
Enter fullscreen mode Exit fullscreen mode

Assigning and updating values

var score = 10;
console.log(score); // 10

score = 20;         // updating the value
console.log(score); // 20
Enter fullscreen mode Exit fullscreen mode

Variables without a value

If you declare a variable but don't assign a value, it holds the special value undefined.

var myVariable;
console.log(myVariable); // undefined
Enter fullscreen mode Exit fullscreen mode

Quick Recap

Concept What it means
Syntax Rules for writing valid JavaScript
Statement A single instruction (often ends with ;)
Comment Notes in code ignored by JavaScript
Variable A named container to store values

Top comments (0)