JavaScript Keywords & Operators: 11 Definitions + Examples

Danielle Ellis
Danielle Ellis

Published:

Keywords are a fundamental part of most programming languages -- and JavaScript is no exception.

person using javascript keywords and operators

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. 

Download Now: An Introduction to Java & JavaScript

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

See the Pen Async Keyword by HubSpot (@hubspot) on CodePen.

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. 

How to Export an Asynchronous Function'

An asynchronous function allows you to execute code outside of the main thread. You can export an asynchronous function by using the async and await keywords. 

Example:

 

 let message = await getMessage();   

 console.log(message);    

   

export default displayMessage;  

Output:

The output will depend on the getMessage() function.

In this example, we use the async and await keywords to export an asynchronous function displayMessage()

2. const

The const or constant keyword creates a value that cannot be changed. 

Const Keyword Example

See the Pen const keyword by HubSpot (@hubspot) on CodePen.

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.

Return Keyword Example

See the Pen Return Keyword by HubSpot (@hubspot) on CodePen.

Output:

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. 

Class Keyword Example

See the Pen class keyword by HubSpot (@hubspot) on CodePen.

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

See the Pen export keyword by HubSpot (@hubspot) on CodePen.

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. 

How to Export Multiple JavaScript Functions

To export multiple functions at the same time, you can use the following expression:

 

export { function1, function2 };   

Output:

In this example, we export two functions simultaneously – function1 and function2. 

6. arguments

The arguments object is used to access the value of an argument passed into a function. 

Get Function Arguments Example

See the Pen Arguments from Function by HubSpot (@hubspot) on CodePen.

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.

Typeof Operator Example

See the Pen Typeof Operator by HubSpot (@hubspot) on CodePen.

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).

Void Operator Example

See the Pen void operator by HubSpot (@hubspot) on CodePen.

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. 

Addition Method Example

See the Pen Addition Method by HubSpot (@hubspot) on CodePen.

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

See the Pen modulo operator by HubSpot (@hubspot) on CodePen.

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

See the Pen Multiplication Operator by HubSpot (@hubspot) on CodePen.

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. 

java

Topics: Javascript

Related Articles

We're committed to your privacy. HubSpot uses the information you provide to us to contact you about our relevant content, products, and services. You may unsubscribe from these communications at any time. For more information, check out our Privacy Policy.

Learn more about one of the world's most popular programming languages.