Keywords are reserved words, which can’t be used as a variable, label, or function name. The keywords have their operation and perform in a certain way to provide different functionality.
Most Common NodeJS Keywords
There are many keywords in Node.js, and in order to understand the fundamental of Node.js, it is important to learn about the most used keywords in detail.
var keyword
This is a keyword used to create variables in Node.js. The scope of this keyword is global or function scope, which means you can call it anywhere inside the function in which it is declared, or you call it anywhere if it is declared outside the function.
Syntax:
var name = value;
let keyword
This keyword is the same as var, but the scope of this function is block-scope. It can not access outside a block in which it is declared.
Syntax:
let name = value;
const keyword
This keyword is used to create a constant whose value can not be changed unlike var and let. The scope of this function also blocks scope.
Syntax:
const name = value;
if/else keyword
If the else keyword is used to execute the statements on the basis of a condition, if the condition is true it will execute the expression in the if block, otherwise executes the statements in the else block.
Syntax:
if(condition){
// statements
}else{
// statements
}
for/while/do keyword
These are the keyword used for creating loops which are used to execute the statements many times under some conditions.
We’ve covered NodeJS Loops in another article if you want to learn more about them.
function keyword
This keyword is used to create functions in Node.js.
Syntax:
function function_name(parameters){
// statements
}
Also read: NodeJS Functions: A Beginner’s Introduction
async/await keyword
Await is used before a statement to hold on and wait for a promise to return. For using await inside a function, it is important to use the async keyword just before the function keyword.
Syntax:
async function name(parameters) {
await promise
}
try/catch/finally keyword
These keywords are used to catch errors. If there is no error, only the try block will execute but, if there are some errors then the statements inside the catch block are executed. You can optionally add the “finally” block which is executed every time after the try or catch blocks are executed. Consider finally as the last thing that the try-catch blog must do before exiting.
Syntax:
try {
tryStatements
} catch (exceptionVar) {
catchStatements
} finally {
finallyStatements
}
then keyword
This keyword works with promise, it uses to return a promise and for method chaining.
Syntax:
const promise1 = new Promise((resolve, reject) => {
resolve('Success!');
});
promise1.then((value) => {
console.log(value);
// expected output: "Success!"
});
export keyword
This keyword is used to export a function or module in Node.js so that it can be used in some other files.
Syntax:
export function_name
require keyword
This keyword is used to import a function or module in Node.js.
Syntax:
require('module_path')
throw keyword
This keyword is used to create a custom error which helps in debugging.
Syntax:
throw statement
class keyword
This keyword is used to create a class in Node.js.
Syntax:
class class_name {
//class_body
}
super keyword
This keyword is used to call the constructor function of the parent class to access its properties and methods.
Syntax:
super.parent_property
this keyword
This keyword is used to call global objects, variables, or functions.
Syntax:
this.global_property
Summary
Keywords are reserve words which means they can’t be used for creating variables, constants, functions, etc. It is important to learn about keywords to master the fundamental of Node.js. We hope this tutorial helped you understand the keywords of Node.js.
Reference
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Grammar_and_Types