Page Content

Tutorials

What are literals in JavaScript And It’s Types

What are called literals?

A data value that shows up immediately in a program is called a literal. Literals stand for fixed values that don’t alter while your scripts run. They are independent values that aren’t kept in a variable. Literals are regarded as primary expressions, which are simple expressions. Knowing literals makes it easier to see how expressions offer homogeneity because literals, variables, and constants are all examples of expressions that produce a value.

Literals in JavaScript

Literals in JavaScript
Literals in JavaScript

A variety of literal kinds are supported by JavaScript, including:

  • Number literals: Numerical values that appear directly in the code are known as number literals. They can be floating-point numbers like 1.2 or integers like 12. Number literals can have underscores (_) as separators.
  • String literals: Text character sequences are represented using string literals. These are made by enclosing the text in either double quotes (“hello world”) or single quotes (“Hi”). Which kind of quote needed to be escaped inside the string depends mostly on whether single or double quotes are used. By default, JavaScript does not process the text contained in string literals; instead, it utilises them literally.
  • Boolean literals: The truth values true or false are represented using boolean literals.
  • Null literal: This represents the absence of an object. Zero is a calculable integer, while null is empty.
  • Regular expression literals, such /javascript/gi, are written directly in the code between slashes and are used for pattern matching. Although escape sequences beginning with a backslash (\) can indicate special characters, characters within usually match themselves literally.
  • Array literals: Object initialisers can be used as array literals, even in more complicated expressions.
  • Object literals: Likewise, object initialisers such as { x:1, y:2 } function as object literals.

In JavaScript, almost everything has a literal value. Although they are not usually called function literals, function expressions can be utilised as values.

One particular kind of string literal is a template literal. They are known as “backtick-quoted strings” because they are surrounded by backticks (`). Compared to conventional string literals, template literals provide the following improved features:

  • Multiline strings: Multiline strings don’t require special characters to spread across many lines.
  • Embedded expressions (Interpolation): The syntax ${expression} allows you to insert JavaScript expressions straight into the literal. The string contains the expression value. For instance, ${x + y} yields the total if x and y are variables, and `half of 100 is ${100 / 2}` evaluates to “half of 100 is 50.”
  • Tagged strings: The “tag function” is a function that can be positioned just before the template literal. Custom processing or the development of Domain Specific Languages (DSLs) are made possible by calling the tag function with the string segments and the values of the embedded expressions. Any value, not only a string, can be returned by a tag function. There is discussion of tag function examples from libraries such as lit-html, regex, and graphql-tag.
  • Raw strings: Tagged templates use the.raw attribute of the first input to the tag function to access “raw” strings without backslashes. The String.raw() tag method returns this raw string.

Strict equality (===) yields true when comparing a template literal with the identical content to a regular string literal.

A syntax tree data structure contains expressions that represent literal texts or numbers in the context of parsing, and a value attribute stores the expressions’ real value.

Abstract equality (==) may apply type coercion when comparing literal types, resulting in unexpected results. This is avoided by strict equality (===), which does not convert types. Type conversion converts 0 == false and “” == false to true, however “” === false is false. One way to determine whether a value has a “real” value is to compare null or undefined with ==.

When non-boolean literals are forced into a boolean environment, the truthy and falsy values idea comes into play. For instance, “false” is truthy, but “” and 0 are false.

Index