JavaScript Syntax
2 April 2025 | Category: JavaScript
What is JavaScript Syntax?
JavaScript syntax refers to the rules and structure of how JavaScript code should be written and interpreted by the browser. If you don’t follow these rules, the JavaScript engine will throw errors.
1️⃣ JavaScript Case Sensitivity
JavaScript is case-sensitive, meaning myVar
and myvar
are treated as different variables.
🔹 Example:
let name = "Alice";
let Name = "Bob";
console.log(name); // Output: Alice
console.log(Name); // Output: Bob
2️⃣ JavaScript Statements
A JavaScript program consists of statements, which are instructions executed by the browser one by one.
🔹 Example:
let a = 10;
let b = 20;
let sum = a + b;
console.log(sum); // Output: 30
✔️ Each statement ends with a semicolon (;
) (optional but recommended).
✔️ JavaScript ignores extra spaces and line breaks.
3️⃣ JavaScript Variables (Declaration & Assignment)
Variables are used to store data in JavaScript.
🔹 Declaring Variables:
let x = 5; // Using let
const y = 10; // Using const (value cannot change)
var z = 15; // Using var (old method, avoid using it)
🔹 Rules for Naming Variables:
✅ Must start with a letter, _
, or $
✅ Cannot start with a number (e.g., 1name
❌)
✅ JavaScript keywords cannot be used as variable names (e.g., let let = 5; ❌
)
4️⃣ JavaScript Data Types
JavaScript has different types of data that variables can hold.
🔹 Example:
let name = "John"; // String
let age = 25; // Number
let isStudent = true; // Boolean
let fruits = ["Apple", "Banana", "Mango"]; // Array
let person = { firstName: "John", lastName: "Doe" }; // Object
let value = null; // Null
let undefinedValue; // Undefined
5️⃣ JavaScript Operators
Operators perform operations on variables and values.
Arithmetic Operators
Used for basic math operations.
let sum = 10 + 5; // Addition (+)
let diff = 10 - 5; // Subtraction (-)
let prod = 10 * 5; // Multiplication (*)
let div = 10 / 5; // Division (/)
let mod = 10 % 3; // Modulus (%)
let power = 2 ** 3; // Exponentiation (2³ = 8)
Comparison Operators
Used for comparing values.
console.log(10 > 5); // true
console.log(10 < 5); // false
console.log(10 == "10"); // true (loose comparison)
console.log(10 === "10"); // false (strict comparison)
Logical Operators
Used to combine conditions.
console.log(true && false); // false (AND)
console.log(true || false); // true (OR)
console.log(!true); // false (NOT)
6️⃣ JavaScript Comments
Comments are used to make code more readable.
🔹 Single-line comment:
// This is a single-line comment
console.log("Hello, JavaScript!");
🔹 Multi-line comment:
/*
This is a
multi-line comment
*/
console.log("Hello, World!");
7️⃣ JavaScript Conditional Statements (If-Else, Switch)
Used to make decisions in code.
🔹 If-Else Statement:
let age = 18;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are a minor.");
}
🔹 Switch Statement:
let fruit = "apple";
switch (fruit) {
case "banana":
console.log("You selected a banana.");
break;
case "apple":
console.log("You selected an apple.");
break;
default:
console.log("Invalid choice.");
}
8️⃣ JavaScript Loops (For, While, Do-While)
Used to repeat a block of code multiple times.
🔹 For Loop:
for (let i = 1; i <= 5; i++) {
console.log("Count: " + i);
}
🔹 While Loop:
let num = 1;
while (num <= 5) {
console.log("Number: " + num);
num++;
}
🔹 Do-While Loop (Runs at least once):
let count = 1;
do {
console.log("Count: " + count);
count++;
} while (count <= 5);
9️⃣ JavaScript Functions
Functions are used to create reusable blocks of code.
🔹 Function Declaration:
function greet(name) {
return "Hello, " + name + "!";
}
console.log(greet("Alice"));
🔹 Arrow Function (ES6+):
const greet = (name) => "Hello, " + name + "!";
console.log(greet("Bob"));
🔟 JavaScript Objects
Objects store key-value pairs.
🔹 Example:
let person = {
firstName: "John",
lastName: "Doe",
age: 30,
greet: function() {
return "Hello, " + this.firstName;
}
};
console.log(person.greet()); // Output: Hello, John
💡 JavaScript Syntax Summary
Concept | Syntax | Example |
---|---|---|
Variables | let , const , var | let name = "Alice"; |
Data Types | String, Number, Boolean, Object, Array | let x = 10; |
Operators | Arithmetic, Comparison, Logical | x > 5 |
Comments | // (Single-line), /* */ (Multi-line) | // This is a comment |
If-Else | if (condition) {} | if (x > 10) {} |
Loops | for , while , do-while | for (let i = 0; i < 5; i++) {} |
Functions | function name() {} | function greet() { return "Hi!"; } |
Objects | { key: value } | let obj = {name: "Alice"}; |
🚀 Conclusion
- JavaScript syntax defines how to write and execute code correctly.
- Follow proper rules for variables, operators, conditions, loops, and functions.
- Writing clean and structured code makes JavaScript easier to read and debug.