🧠 What is Syntax?
In programming, syntax is the set of rules that defines how code must be written to be understood by the interpreter or compiler. JavaScript has its own syntax rules that are simple but powerful.
🔤 Basic Syntax Elements
- 📦 Variables – containers for storing data values
- 🧪 Operators – used to assign, compare, or perform operations on values
- 🔁 Statements – individual instructions that perform actions
- 📚 Blocks – groups of code enclosed in {}
- 🎯 Semicolons – optional line terminators
📦 Declaring Variables
Use var, let, or const to declare variables:
Variable Declarations
var name = "Alice"; // Old way
let age = 25; // Recommended
const pi = 3.14; // Constant valueNote
➕ Operators
Operators are used to perform actions on variables and values.
Common Operators
let a = 10;
let b = 5;
console.log(a + b); // Addition
console.log(a - b); // Subtraction
console.log(a * b); // Multiplication
console.log(a / b); // Division
console.log(a % b); // Modulus (remainder)🧾 JavaScript Statements
Statements are instructions executed by the browser. Each line of code is a statement:
Basic Statements
let message = "Hello!";
console.log(message); // Outputs: Hello!Note
🧱 Code Blocks and Scope
Use curly braces {} to group multiple statements into one block, often seen in functions or conditionals:
Block Example
{
let name = "Block";
console.log(name);
}🎯 Comments
Use comments to document code or temporarily disable parts of it.
Comment Syntax
// This is a single-line comment
/*
This is a multi-line comment
Useful for explaining longer blocks
*/📚 String Syntax
Strings in JavaScript can be written using "double quotes", 'single quotes', or `template literals`.
String Examples
let a = "Hello";
let b = 'World';
let c = `${a}, ${b}!`; // Hello, World!Note
🧠 Summary
JavaScript syntax includes everything from declaring variables and writing expressions to grouping code and commenting. Learning the syntax properly helps you write cleaner, bug-free code and communicate your logic effectively.
- ✅ Use let/const for modern variable declarations
- ✅ Master operators and expressions
- ✅ Understand blocks and statement grouping
- ✅ Use comments to document and explain code