Keywords are a fundamental part of most programming languages -- and JavaScript is no exception.
Keywords are the building blocks of JavaScript, and they are used in conjunction with functions, variables, and objects to execute different tasks on your web page or software.
As a JavaScript programmer, you'll want to familiarize yourself with these keywords and how you can make best use of them. That's why we've put together this list of JavaScript keywords along with some examples of each that you can test out on your own. We've also included a list of JavaScript operators that are commonly used with several of these keywords.
Read on for a comparison between keywords and operators, or use the jump links below to find exactly what you are looking for.
Table of Contents:
JavaScript Keywords vs. Operators
In JavaScript, a keyword is a reserved word that can't indicate variable labels or function names. However, they do perform internal operations like labeling objects as a constant.
Operators are like keywords, but instead of being actual words, they are mostly symbols like % and +. Operators are used to solve operation problems like addition, multiplication, and subtraction.
JavaScript Keywords
1. Async
The async keyword creates an asynchronous function that returns a promise and can contain await expressions.
Async Keyword Example
Output:
{data: {…}, status: 200, statusText: "OK"}
In this example, the async keyword is used to create an asynchronous function that returns a promise that contains the response from a fetch call.
2. const
The const (constant) keyword creates a value that cannot be changed.
Output:
3.14
In this example, the constant keyword is used to declare PI as a constant with a value of 3.14 that cannot be changed.
3. return
The return keyword is used to return a value from a function.
Output:
5
In this example, the return keyword returns the result of adding two numbers - 2 and 3 - and the result is 5.
4. class
The class keyword is used to create a class in JavaScript. A class is a template for an object, and the syntax of a class consists of methods and properties related to the object.
Output:
HubSpot
In this example, the class keyword defines a class for an object called Company with a name property. The sayName() method is then used for logging the company's name, ‘HubSpot’.
JavaScript Class vs. Function
In JavaScript, a class is a special type of function that is built on a prototype. Unlike a function, classes are used to create objects which contain both code and data.
5. export
The export keyword makes a function or object available to other modules in the same project.
Export Keyword Example
Output:
Hello Reader!
In this example, the export keyword makes the sayHello() function available to other modules in the same project. The function then prints out 'Hello Reader!' when called.
6. arguments
The arguments object is used to access the value of an argument passed into a function.
Output:
{ '0': 'apple', '1': 'banana' }
In this example, the arguments object is used to access the values of two arguments (apple and banana) passed into a function (myFunction). The output is then printed out on the console.
JavaScript Operators
1. typeof
The typeof operator returns the data type for a given variable or value.
Output:
String
In this example, the typeof operator returns the data type for the variable ‘str’ as a string.
2. void
The void operator evaluates an expression that isn’t returning a value (undefined).
Output:
undefined
In this example, the void operator is used to evaluate an expression (0 in this case) and then return undefined. The output is then printed out on the console.
3. Addition: +
The addition operator finds the sum of two or more numbers.
Output:
13
In this example, the addition operator calculates the sum of two numbers (10 and 3). The output is then printed out on the console.
4. Modulo: %
The modulo operator returns the remainder value of a division equation.
Modulo Operator Example
Output:
In this example, the modulo operator ( % ) gets the remainder after dividing two numbers (10 and 3). The output is then printed out on the console.
5. Multiplication: *
The multiplication operator is used to find the product of two numbers.
Multiplication Operator Example
Output:
In this example, the multiplication operator ( * ) is used to find the product of two numbers (10 and 3). The output is then printed out on the console.