New Tutorials:   TKINTER    KOTLIN    JAVASCRIPT    SASS/SCSS    PL/SQL
See the Tutorial List

JavaScript Literals and Keywords

JavaScript Literals are the fixed value that cannot be changed, you do not need to specify any type of keyword to write literals. Literals are often used to initialize variables in programming, names of variables are string literals.

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

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

JavaScript Numeric Literal

  • It can be expressed in the decimal(base 10), hexadecimal(base 16) or octal(base 8) format.

  • Decimal numeric literals consist of a sequence of digits (0-9) without a leading 0(zero).

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

JavaScript Numeric Literals Example

120 // decimal literal

021434 // octal literal

0x4567 // hexadecimal literal

JavaScript Floating-Point Literal

  • It contains a decimal point(.)

  • A fraction is a floating-point literal

  • It may contain an Exponent.

JavaScript Floating-Point Literal Example

6.99689 // floating-point literal

-167.39894 // negative floating-point literal

JavaScript Boolean Literal

Boolean literal supports two values only either true or false.

JavaScript Boolean Literal Example

true // Boolean literal

false // Boolean literal

JavaScript String Literal

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

JavaScript String Literal Example

"Study" // String literal

'tonight' // String literal 

String literals can have some special characters too which are tabled below.

String Special Characters:

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

JavaScript Array Literal

  • An array literal is a list of zero or more expressions representing array elements that are enclosed in a square bracket([]).

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

JavaScript Array Literal Example

var emp = ["aman","anu","charita"];  // Array literal

JavaScript Regular Expression Literal

Regular Expression is a pattern, 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

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

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

JavaScript Object Literal

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

JavaScript Object Literal Example

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

JavaScript Keywords

Every programming language has its own keywords and reserved words. Every keyword is created to perform a specific task and the compiler or interpreter already knows about the built-in keywords and reserved words. JavaScript supports a rich set of keywords that are listed in the below table.

Keyword Description
for The for keyword is used to create a for-loop.
do/while The 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.
float It is used to declare float type variable.
int It is used to declare int type variable.
private It is an access modifier.
public Public is an access modifier that gives the class public access.
var Var is 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 creates a block for error handling of the statements.

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

In this tutorial, we learned about JavaScript literals, different types of literals along with examples. We also covered the JavaScript keywords and reserved words.