Learn JavaScript syntax, including statements, values, variables, operators, expressions, keywords, identifiers, comments, and code blocks.
JavaScript syntax means the set of rules used to write JavaScript code correctly.
Just like English has grammar rules, JavaScript also has rules. If we do not follow these rules, the browser may show an error or the code may not work properly.
JavaScript syntax includes statements, variables, values, operators, functions, comments, and code blocks.
What is JavaScript Syntax?
JavaScript syntax defines how JavaScript programs are written.
Example:
Example
javascript
let message ="Hello JavaScript";
document.write(message);
A statement is an instruction that tells the browser what to do.
Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Statements</title></head><body><h2>JavaScript Statements</h2><script>
let name = "Amit";
document.write(name);
</script></body></html>
In this example, there are two JavaScript statements:
Example
javascript
let name ="Amit";
document.write(name);
The first statement creates a variable, and the second statement displays the value.
In JavaScript, variables can be declared using let, const, or var.
Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Variables</title></head><body><h2>JavaScript Variables</h2><script>
let course = "JavaScript";
document.write(course);
</script></body></html>
Here, course is a variable, and it stores the value "JavaScript".
JavaScript Operators
Operators are used to perform operations on values and variables.
Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Operators</title></head><body><h2>JavaScript Operators</h2><script>
let a = 10;
let b = 5;
let result = a + b;
document.write(result);
</script></body></html>
In this example, the + operator adds two values.
Common JavaScript operators:
Operator
Use
<code>+</code>
Addition
<code>-</code>
Subtraction
<code>*</code>
Multiplication
<code>/</code>
Division
<code>=</code>
Assignment
JavaScript Expressions
An expression is a combination of values, variables, and operators that produces a result.
Example
Example
javascript
let total =10+20;
Here, 10 + 20 is an expression.
Another example:
Example
javascript
let fullName ="Amit"+" "+"Kumar";
Here, strings are joined using the + operator.
JavaScript Keywords
JavaScript keywords are reserved words that have special meaning.
Examples of JavaScript keywords:
Keyword
Use
<code>let</code>
Declares a variable
<code>const</code>
Declares a constant variable
<code>var</code>
Declares a variable
<code>function</code>
Defines a function
<code>if</code>
Creates a condition
<code>else</code>
Runs code when condition is false
<code>return</code>
Returns a value from a function
You cannot use keywords as variable names.
Wrong example:
Wrong Example — this does not run
javascript
letfunction="test";
Correct example:
Example
javascript
let functionName ="test";
JavaScript Identifiers
Identifiers are names used for variables, functions, and objects.
Example:
Example
javascript
let studentName ="Rahul";
Here, studentName is an identifier.
Rules for JavaScript Identifiers
JavaScript identifiers must follow these rules:
Rule
Example
Can contain letters
name
Can contain digits, but not at the beginning
name1
Can contain underscore <code>_</code>
student_name
Can contain dollar sign <code>$</code>
$price
Cannot contain spaces
studentName
Cannot be a JavaScript keyword
let, function, if
Wrong examples:
Wrong Example — this does not run
javascript
let 1name ="Amit";let student name ="Rahul";letif="test";
Correct examples:
Example
javascript
let name1 ="Amit";let studentName ="Rahul";let userAge =20;
JavaScript is Case-Sensitive
JavaScript treats uppercase and lowercase letters differently.
Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Case Sensitive</title></head><body><h2>JavaScript is Case-Sensitive</h2><script>
let name = "Amit";
let Name = "Rahul";
document.write(name + "<br>");
document.write(Name);
</script></body></html>
In this example, name and Name are two different variables.
JavaScript Semicolons
Semicolons are used to separate JavaScript statements.
Example:
Example
javascript
let name ="Amit";let age =20;
document.write(name);
JavaScript can sometimes work without semicolons, but using semicolons is a good practice for beginners.
JavaScript Code Blocks
A code block is a group of statements written inside curly braces {}.
Code blocks are commonly used with functions, conditions, and loops.
Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Code Block</title></head><body><h2>JavaScript Code Block</h2><buttononclick="showMessage()">Click Me</button><pid="demo"></p><script>
function showMessage() {
document.getElementById("demo").innerHTML = "This code is inside a block.";
}
</script></body></html>
In this example, the function body is written inside {}.
JavaScript Comments
Comments are used to explain code. Comments are ignored by the browser.
JavaScript supports two types of comments:
Comment Type
Syntax
Single-line comment
// comment
Multi-line comment
/<em>comment</em>/
Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Comments</title></head><body><h2>JavaScript Comments</h2><script>
// This is a single-line comment
document.write("Hello JavaScript");
/*
This is a multi-line comment.
It can be written in more than one line.
*/
</script></body></html>
Complete Example
Example
html
<!DOCTYPEhtml><html><head><title>JavaScript Syntax Example</title></head><body><h1>JavaScript Syntax</h1><pid="demo"></p><buttononclick="showResult()">Show Result</button><script>
function showResult() {
let firstNumber = 10;
let secondNumber = 20;
let total = firstNumber + secondNumber;
document.getElementById("demo").innerHTML = "Total: " + total;
}
</script></body></html>
Try It Yourself
Run the above examples in the Try It Editor.
Try changing variable names, text values, and numbers to see how JavaScript syntax works.
Important Notes
JavaScript syntax must be written carefully.
JavaScript is case-sensitive, so name and Name are different.
Strings should be written inside quotes.
JavaScript statements usually end with semicolons.
Code blocks are written inside curly braces {}.
Common Mistakes
Beginners often forget quotes around text values.
Wrong example:
Example
javascript
let name = Amit;
Correct example:
Example
javascript
let name ="Amit";
Another common mistake is writing wrong capitalization.
Wrong example:
Example
javascript
document.getElementByID("demo");
Correct example:
Example
javascript
document.getElementById("demo");
Also, do not use JavaScript keywords as variable names.
Conclusion
JavaScript syntax is the set of rules for writing JavaScript code.
It includes statements, values, variables, operators, expressions, keywords, identifiers, comments, and code blocks.
Learning JavaScript syntax properly is the first step toward writing clean and error-free JavaScript programs.