JavaScript Syntax and Basics

Syntax is the set of rules that defines how JavaScript code must be written. Just like every spoken language has grammar rules, every programming language has syntax rules. If the syntax is wrong, the code will not work.

Understanding JavaScript syntax from the beginning helps avoid common mistakes and builds a strong foundation for everything that follows.

JavaScript Statements

A statement is a single instruction that tells the browser to do something. JavaScript programs are made up of a series of statements.

console.log("Good morning");
console.log("JavaScript is fun");
console.log("Let's learn step by step");

Each line above is a separate statement. The browser executes them one by one, from top to bottom.

Semicolons

A semicolon ; marks the end of a statement. JavaScript does not always require semicolons (it can figure out where statements end), but it is a good habit to include them. It prevents unexpected errors.

let name = "Alice";
let age = 25;
console.log(name);

Case Sensitivity

JavaScript is case-sensitive. This means name, Name, and NAME are three completely different things.

let color = "blue";
let Color = "red";

console.log(color);  // blue
console.log(Color);  // red

Always be careful with uppercase and lowercase letters, especially when naming variables and calling functions.

Whitespace and Indentation

JavaScript ignores extra spaces and blank lines. However, adding proper indentation (spacing) makes code easier to read.

// Hard to read
if(true){console.log("Yes");}

// Easy to read
if (true) {
  console.log("Yes");
}

Good indentation is a professional habit that every developer should follow.

Comments in JavaScript

Comments are lines of text that JavaScript ignores during execution. They are used to explain what the code does — helpful for anyone reading the code later (including yourself).

Single-line Comment

Use // to write a comment on one line.

// This line prints a greeting message
console.log("Hello!");

Multi-line Comment

Use /* ... */ to write comments that span multiple lines.

/*
  This program calculates the total price.
  It multiplies quantity by price per item.
*/
let quantity = 5;
let price = 20;
let total = quantity * price;
console.log(total);  // 100

Console Output Methods

The browser console is the main tool used to test and debug JavaScript code. There are several ways to output information:

console.log()

The most commonly used method. Prints any value to the console.

console.log("Hello World");
console.log(42);
console.log(true);

console.warn()

Prints a warning message in yellow color in the console.

console.warn("This is a warning");

console.error()

Prints an error message in red color in the console.

console.error("Something went wrong!");

alert()

Shows a popup message box in the browser.

alert("Welcome to our website!");

JavaScript Keywords

Keywords are reserved words that have special meaning in JavaScript. They cannot be used as variable names or function names.

Some common JavaScript keywords:

KeywordPurpose
varDeclare a variable (old style)
letDeclare a block-scoped variable
constDeclare a constant value
if / elseConditional logic
for / whileLoops
functionDefine a function
returnReturn a value from a function
true / falseBoolean values
nullRepresents no value
typeofCheck the type of a value

Identifiers (Naming Rules)

An identifier is any name given to a variable, function, or label in JavaScript. There are specific rules for naming identifiers:

  • Must start with a letter, underscore _, or dollar sign $
  • Cannot start with a number
  • Can only contain letters, numbers, underscores, or dollar signs
  • Cannot use reserved keywords
  • Are case-sensitive
// Valid identifiers
let userName = "Bob";
let _count = 10;
let $price = 99;

// Invalid identifiers
// let 1name = "error";   // Cannot start with number
// let let = "error";     // Cannot use keyword

Naming Conventions

JavaScript developers typically follow these naming styles:

camelCase (most common for variables and functions)

let firstName = "John";
let totalAmount = 500;
function calculateTax() {}

PascalCase (used for classes)

class ShoppingCart {}
class UserProfile {}

UPPER_SNAKE_CASE (used for constants)

const MAX_SPEED = 120;
const TAX_RATE = 0.18;

Code Blocks

A code block is a group of statements wrapped inside curly braces { }. Blocks are used with functions, loops, and conditionals to group related code together.

if (true) {
  // This is a code block
  let message = "Block executed";
  console.log(message);
}

A Complete Example

// Declare variables
let studentName = "Priya";
let score = 85;

// Check the score and display a message
if (score >= 50) {
  console.log(studentName + " has passed the exam.");
} else {
  console.log(studentName + " needs to study more.");
}

// Output: Priya has passed the exam.

Key Points to Remember

  • JavaScript code is made up of statements, executed top to bottom
  • Semicolons end statements — use them consistently
  • JavaScript is case-sensitive — always match capitalization exactly
  • Comments explain code and are ignored by JavaScript
  • Use console.log() to print output while learning and debugging
  • Follow camelCase naming for variables and functions
  • Code blocks use curly braces { } to group statements together

Leave a Comment

Your email address will not be published. Required fields are marked *