JavaScript Syntax
JavaScript syntax is the set of rules that defines how JavaScript programs are written. Understanding syntax is important because even small mistakes can cause errors in your code.
JavaScript syntax is simple and beginner-friendly. Once you learn the basic structure, writing programs becomes much easier.
Basic JavaScript Syntax
A simple JavaScript statement looks like this:
console.log("Welcome to JavaScript");
This statement prints a message inside the browser console.
JavaScript Statements
JavaScript programs are made up of statements. Statements are instructions that the browser executes one by one.
let name = "Ali";
console.log(name);
alert("Hello");
Explanation
- let name = "Ali"; creates a variable.
- console.log(name); displays output in the console.
- alert("Hello"); shows a popup message.
JavaScript is Case Sensitive
JavaScript treats uppercase and lowercase letters differently.
let userName = "Ali"; let username = "Ahmed"; console.log(userName); console.log(username);
Both variables are different because JavaScript is case-sensitive.
Semicolons in JavaScript
Semicolons are used to end statements. Although JavaScript can automatically insert semicolons, it is considered a good practice to write them manually.
let age = 20; console.log(age);
JavaScript Comments
Comments are used to explain code. Comments are ignored by JavaScript.
Single Line Comment
// This is a single line comment
console.log("Hello");
Multi Line Comment
/*
This is a
multi-line comment
*/
console.log("JavaScript");
JavaScript Variables
Variables are used to store data values.
let city = "Delhi"; const country = "India"; var marks = 90;
Explanation of Variable Keywords
- let is used for variables that can change.
- const is used for fixed values.
- var is the older way of declaring variables.
Whitespace and Formatting
JavaScript ignores extra spaces and line breaks. However, proper formatting makes code easier to read.
let firstName = "Ali"; let lastName = "Khan"; console.log(firstName + " " + lastName);
Code Blocks
Code blocks are written inside curly braces {}. They are commonly used in functions, loops, and conditions.
if (10 > 5) {
console.log("10 is greater");
}
JavaScript Keywords
Keywords are reserved words with special meanings in JavaScript.
- let
- const
- var
- if
- else
- for
- while
- function
- return
JavaScript Naming Rules
Variable and function names must follow certain rules.
- Names can contain letters, numbers, _ and $.
- Names cannot start with numbers.
- Spaces are not allowed.
- Reserved keywords cannot be used as variable names.
- Use meaningful names for better readability.
Example Program
let studentName = "Rahul";
let marks = 85;
console.log(studentName);
console.log(marks);
alert("Student Data Loaded");
Output
Rahul 85 Popup Message: Student Data Loaded
Best Practices for Writing Syntax
- Use proper indentation.
- Write meaningful variable names.
- Use semicolons consistently.
- Keep code clean and readable.
- Add comments when necessary.
Summary
- JavaScript syntax defines how code is written.
- Statements are instructions executed by JavaScript.
- JavaScript is case-sensitive.
- Semicolons improve code readability.
- Comments help explain the code.
- Variables store data values.
- Proper formatting makes code easier to understand.