JavaScript Syntax
Learn JavaScript syntax in a simple way — understand statements, comments, variables, values, and operators with easy-to-follow examples.
Contents
- Introduction
- Statements
- Comments
- Variables
- Values
- Operators
- Case Sensitive
- Naming Rules
- Best Practices
- FAQ
JavaScript Syntax Introduction
Syntax means the rules and structure of a programming language. JavaScript syntax tells you how to write code correctly so the browser understands it. Learning syntax is like learning the grammar of English — you need to follow the rules to communicate clearly.
Why syntax matters
Without proper syntax, your code won't work. The browser won't understand what you're trying to do and will show errors instead.
Statements
A statement is a single instruction that tells JavaScript to do something. Every statement usually ends with a semicolon ;
Statement examples
let name = "John"; // Statement 1 let age = 25; // Statement 2 alert("Hello"); // Statement 3
Comments
Comments explain your code. They don't execute, they just help you and others understand what the code does. There are two types of comments in JavaScript.
Single-line comments
Use // to write a comment on one line:
// This is a comment let x = 10; // Variable x equals 10
Multi-line comments
Use /* */ for comments that span multiple lines:
/* This is a multi-line comment It can span multiple lines Use it for longer explanations */ let age = 25;
Variables
A variable is like a labeled box that holds data. You create variables using let, const, or var.
Creating variables
// Using let (modern, recommended) let name = "John"; // Using const (cannot be changed) const PI = 3.14159; // Using var (old, avoid in new code) var age = 25;
Variable differences
Values
A value is the actual data you put into a variable. JavaScript values can be text, numbers, true/false, or more complex data.
Types of values
let text = "Hello"; // String (text) let number = 42; // Number let decimal = 3.14; // Decimal number let isTrue = true; // Boolean let empty = undefined; // Empty value
Operators
Operators are symbols that perform actions on values. They let you do math, compare values, or combine text.
Common operator types
Operator examples
let x = 10; let y = 5; let sum = x + y; // 15 let product = x * y; // 50 let text = "Hello" + " World"; // "Hello World"
Case Sensitive
JavaScript is case sensitive, which means it treats uppercase and lowercase letters as different. Name and name are not the same.
Case sensitive examples
let name = "John"; // Correct let Name = "Jane"; // Different variable alert(name); // Shows "John" alert(Name); // Shows "Jane"
Variable Naming Rules
When naming variables, follow these rules to avoid errors and make your code readable:
Rules for naming
- Start with a letter, underscore, or dollar sign — Never start with a number
- Only use letters, numbers, underscores, and dollar signs — No spaces or special characters
- Use camelCase for multiple words — myVariableName (not my_variable_name)
- Use meaningful names — Use userName, not u or x
- Avoid JavaScript keywords — Don't use let, function, if, etc. as variable names
Good vs bad naming
// Good variable names let userName = "John"; let userAge = 25; let isActive = true; // Bad variable names (avoid these) let u = "John"; // Too short let user age = 25; // Space in name (error!) let 2fast = true; // Starts with number
Best Practices
Frequently Asked Questions
JavaScript Syntax is the set of rules for writing JavaScript code. It includes how to write statements, comments, variables, and operators correctly.
A statement is a single instruction that tells JavaScript to do something. Statements usually end with a semicolon.
Comments explain your code. Single-line comments use // and multi-line comments use /* */. Comments don't execute.
let allows variables to change, while const creates variables that cannot be changed after creation. Use const by default, let when you need to change values.
Yes, JavaScript is case sensitive. "Name" and "name" are different variables. Always match the exact case.
Use meaningful names in camelCase (userName, userAge). Start with a letter, underscore, or dollar sign. Never start with numbers.