- Home
- /
- Tutorials
- /
- JavaScript Tutorial
- /
- JavaScript Keywords
JavaScript Statement & Expression
JavaScript Keywords
JavaScript keywords are reserved words that have a special meaning in JavaScript.
These words are already used by JavaScript for specific tasks, so you cannot use them as variable names, function names, or object names.
Example
Example
javascript
let name = "Amit";Here, let is a JavaScript keyword. It is used to declare a variable.
What are Keywords?
Keywords are special words that JavaScript understands.
Each keyword has a fixed purpose.
Example
javascript
if (age >= 18) {
console.log("You are eligible.");
}In this example:
| Keyword | Use |
|---|---|
if | Checks a condition |
The word if has a special meaning in JavaScript, so it cannot be used as a variable name.
Common JavaScript Keywords
Here are some commonly used JavaScript keywords:
| Keyword | Use |
|---|---|
let | Declares a variable |
const | Declares a constant variable |
var | Declares a variable |
if | Starts a condition |
else | Runs code when condition is false |
switch | Checks multiple cases |
case | Defines a case inside switch |
break | Stops a loop or switch |
for | Starts a for loop |
while | Starts a while loop |
function | Defines a function |
return | Returns a value from a function |
class | Defines a class |
new | Creates a new object |
try | Tests a block of code |
catch | Handles errors |
import | Imports code from another file |
export | Exports code to another file |
Keywords for Variables
JavaScript uses let, const, and var to declare variables.
Example
javascript
let userName = "Amit";
const country = "India";
var city = "Delhi";In modern JavaScript, let and const are used more commonly than var.
Keywords for Conditions
JavaScript uses keywords like if, else, and switch for conditions.
Example
javascript
let age = 20;
if (age >= 18) {
console.log("Adult");
} else {
console.log("Minor");
}Here, if checks the condition, and else runs when the condition is false.
Keywords for Loops
JavaScript uses keywords like for, while, and break with loops.
Example
javascript
for (let i = 1; i <= 3; i++) {
console.log(i);
}Here, for is used to repeat code.
The break keyword can stop a loop.
Example
javascript
for (let i = 1; i <= 5; i++) {
if (i === 3) {
break;
}
console.log(i);
}Keywords for Functions
JavaScript uses function and return to create and use functions.
Example
javascript
function add(a, b) {
return a + b;
}
let result = add(10, 20);Here:
| Keyword | Use |
|---|---|
function | Creates a function |
return | Sends a value back from the function |
Keywords Cannot Be Used as Names
You cannot use JavaScript keywords as variable names.
Wrong example:
Wrong Example
javascript
let if = "Hello";
let function = "Test";
let return = 100;These names are not allowed because if, function, and return are reserved keywords.
Correct example:
Correct Example
javascript
let userStatus = "Hello";
let functionName = "Test";
let returnValue = 100;Use meaningful names that are not JavaScript keywords.
Complete Example
Complete Example
html
<!DOCTYPE html>
<html>
<head>
<title>JavaScript Keywords</title>
</head>
<body>
<h1>JavaScript Keywords</h1>
<p id="result"></p>
<script>
let age = 20;
let message;
if (age >= 18) {
message = "You are eligible.";
} else {
message = "You are not eligible.";
}
document.getElementById("result").innerHTML = message;
</script>
</body>
</html>Try It Yourself
Run the above example in the Try It Editor.
Try changing the value of age and check how the result changes.
Also try using different keywords like const, function, and return.
Important Points
- JavaScript keywords are reserved words.
- Each keyword has a special meaning.
- Keywords cannot be used as variable names.
- Common keywords include
let,const,if,else,for,while,function, andreturn. - Use meaningful names for variables and functions instead of reserved keywords.
Conclusion
JavaScript keywords are special reserved words used to perform specific tasks.
They help JavaScript create variables, check conditions, run loops, define functions, handle errors, and work with modules.
Understanding keywords is important because they are used in almost every JavaScript program.
