Signup/Sign In
LAST UPDATED: MARCH 19, 2024

JavaScript Literals and Keywords

Literals in JavaScript means values. When you create a variable in JavaScript, you assign it a value, that value is a literal. Literal is a fixed value that cannot be changed, you do not need to specify any keyword along with it.

Literals are often used to initialize variables in programming, even the names of variables are literals - a string literal.

A JavaScript Literal can be a numeric, string, floating-point value, a boolean value, an array, or an object. In simple words, any value is a literal, if you write a string "Studytonight" - that is a literal, any number like 7007 is also a literal, etc.

JS literal example

JavaScript supports various types of literals which are listed below:

  • Numeric Literal

  • Floating-point Literal

  • Boolean Literal

  • String Literal

  • Array Literal

  • Regular Expression Literal

  • Object Literal

Let's see some code examples to see each one of these.

Numeric Literal in JS

  • It can be a decimal value(base 10), a hexadecimal value(base 16), or an octal value(base 8).

  • Decimal numeric literals consist of a sequence of digits (0-9) without a leading 0(zero). These are integer values that you will be using mostly in your JS code.

  • Hexadecimal numeric literals include digits(0-9), letters (a-f) or (A-F).

  • Octal numeric literals include digits (0-7). A leading 0(zero) in a numeric literal indicates octal format.

Numeric Literals Example:

Here is an example of the different types of numeric literals.

120 // decimal literal

021434 // octal literal

0x4567 // hexadecimal literal

Floating-Point Literal in JS

  • It contains a decimal point, for example, the value 1.234

  • A fractional value is a floating-point literal.

  • It may contain an Exponent.

Floating-Point Literal Example:

Here is an example of floating point literals.

6.99689 // floating-point literal

-167.39894 // negative floating-point literal

Boolean Literal in JS

Boolean literal can have two values, either true or false.

true // Boolean literal

false // Boolean literal

String Literal in JS

A string literal is a combination of characters (alphabets or numbers or special characters) enclosed within single('') or double quotation marks ("").

"Study" // String literal

'tonight' // String literal 

String literals can have some special characters too which are listed in the table below.

Character Description

\b

It represents a backspace.
\f It represents a Form Feed.
\n It represents a new line.
\r It represents a carriage return.
\t It represents a tab.
\v It represents a vertical tab.
\' It represents an apostrophe or a single quote.
\" It represents a double quote.
\\ It represents a backslash character.
\uXXXX It represents a Unicode character specified by a four-digit hexadecimal number.

Array Literal in JS

  • An array literal is an array in JavaScript created using the square bracket ([]) with or without values.

  • Whenever you create an array using an array literal, it is initialized with the elements specified in the square bracket.

JavaScript Array Literal Example:

Here is a quick example,

["Abhishek","Supriya","Joey"];  // Array literal

Even when you assign an array to a variable, if you create the array directly using the square brackets, it is an array literal.

let students = ["Abhishek","Supriya","Joey"];  // Array literal

In JavaScript, you can create an array using the Array object or using an array literal.

Regular Expression Literal in JS

Regular Expression is a bunch of characters defining a pattern, that is used to match a character or string in some text. It is created by enclosing the regular expression string between forward slashes.

JavaScript Regular Expression Example:

Here is an example,

var myregexp = /ab+c/; // Regular Expression literal

var myregexp = new RegExp("abc"); // Regular Expression object

Object Literal in JS

It is a collection of key-value pairs enclosed in curly braces ({}). The key-value pairs are separated using a comma.

JavaScript Object Literal Example:

Here is an example,

var games = {cricket :11, chess :2, carom: 4}  // Object literal

In the code example above, on the right side of the equals to or assignment operator, we have the object literal.

JavaScript Keywords

Every programming language has its keywords or reserved words. Every keyword is created to perform a specific task or to be used for a specific purpose, which is known to the compiler or the interpreter. JavaScript supports a rich set of keywords, listed in the below table.

Keyword Description
for The for keyword is used to create a for loop.
do/while They do and while both keywords are used to create loops in JavaScript.
if/else The if and else keywords are used to create conditional statements.
continue The continue keyword is used to resume the loop.
break It is used to break the loop.
function The function keyword is used to declare a function.
debugger It is used to call the debugger function
class The class keyword is used to declare the class.
return Return keyword is used to return function from the function.
export Used to export some functions, variables, etc. from a module
var, let, const The var, let and const keywords are used to declare a variable.
switch The switch creates various statement blocks and executes only on block depending on the condition or the case.
try/catch It is used to create a block for error handling of the statements.

In the table above we have mentioned some keywords. There are many other keywords as well in JavaScript.

NOTE: While defining the name of any function or a variable or class, you should not use any keyword or reserved words.

For example,

let for = 10
console.log(for)


let for = 10
^^^

SyntaxError: Unexpected token 'for'

You get an error because for cannot be used as a variable name.



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight