Literal in JavaScript
In JavaScript, literals are a way to represent fixed values directly in the code. They are used to create arrays, objects, strings, and other data types without the need for constructors or functions. This page explores different types of literals in JavaScript, including array literals, template literals, and object literals, with examples and explanations.
Array Literals
An array literal is a way to create an array directly by listing its elements within square brackets. For example:
const ninjas = ['Kuma', 'Hattori', 'Yagyu'];
In this example, the array ninjas is created with three elements: 'Kuma', 'Hattori', and 'Yagyu'.
Template Literals
Template literals, introduced in ES6, provide a more powerful way to work with strings. They are enclosed by backticks (`) and can contain placeholders for variables and expressions, which are denoted by the ${} syntax. This allows for dynamic string construction without the need for cumbersome concatenation.
Example of Template Literals
Listing A.1. Template literals
const ninja = {
name: "Yoshi",
action: "subterfuge"
};
const concatMessage = "Name: " + ninja.name + " "
+ "Action: " + ninja.action;
const templateMessage = `Name: ${ninja.name} Action: ${ninja.action}`; #A
assert(concatMessage === templateMessage,
"Our messages match");
#A - Uses backticks to create template literals that can contain JavaScript expressions encapsulated in ${}
In this example, templateMessage is constructed using a template literal, which makes the code cleaner and more readable compared to the traditional concatenation method used in concatMessage.
Multiline Template Literals
Template literals also support multiline strings, which is not possible with standard single or double-quoted strings.
Listing A.2. Multiline template literals
const name = "Yoshi", action = "subterfuge";
const multilineString =
`Name: ${name}
Yoshi: ${action}`; #A
#A - Template strings aren’t limited to a single line.
Here, multilineString spans multiple lines, demonstrating the flexibility of template literals.
Object Literals
Object literals are a convenient way to create objects in JavaScript. They allow you to define properties and methods within curly braces. ES6 introduced enhancements to the object literal syntax, making it even more powerful.
Enhanced Object Literals
Listing A.5. Enhanced object literals
const name = "Yoshi";
const oldNinja = {
name: name, #A
getName: function(){ #B
return this.name;
}
};
oldNinja["old" + name] = true; #C
assert(oldNinja.name === "Yoshi", "Yoshi here");
assert(typeof oldNinja.getName === "function", "with a method");
assert("oldYoshi" in oldNinja, "and a dynamic property");
const newNinja = {
name, #D
getName(){ #E
return this.name;
},
["new" + name]: true #F
};
assert(newNinja.name === "Yoshi", "Yoshi here, again");
assert(typeof newNinja.getName === "function", "with a method");
assert("newYoshi" in newNinja, "and a dynamic property");
#A - Creates a property with the same name as a variable in scope and assigns the value of that variable to it
#B - Defines a method on an object
#C - Creates a property whose name is dynamically calculated
#D - Property value shorthand syntax; assigns the value of the same named variable to the property
#E - Method definition shorthand; there’s no need to add a colon and the function keyword. Using parentheses after the property name signals that we’re dealing with a method.
#F - A computed property name
This example demonstrates the creation of oldNinja and newNinja objects. The newNinja object uses ES6 shorthand syntax for property values and methods, as well as computed property names, making the code more concise and expressive.