Movatterモバイル変換


[0]ホーム

URL:


  1. Glossary
  2. Literal

Literal

Literals represent values in JavaScript. These are fixed values—not variables—that youliterally provide in your script.

Examples

String literals

A string literal is zero or more characters enclosed in double (") or single quotation marks ('). A string must be delimited by quotation marks of the same type (that is, either both single quotation marks, or both double quotation marks).

The following are examples of string literals:

js
"foo";"bar";"1234";"one line \n new line";"Joyo's cat";

Object literals

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

The following is an example of an object literal. The first element of thecar object defines a property,myCar, and assigns to it a new string,"Toyota"; the second element, thegetCar property, is immediately assigned the result of invoking the functioncarTypes('Honda'); the third element, thespecial property, uses an existing variable (sales).

js
const sales = "BMW";function carTypes(name) {  return name === "Honda" ? name : `Sorry, we don't sell ${name}.`;}const car = {  myCar: "Toyota",  getCar: carTypes("Honda"),  special: sales,};console.log(car.myCar); // Toyotaconsole.log(car.getCar); // Hondaconsole.log(car.special); // BMW

See also

Help improve MDN

Learn how to contribute

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2026 Movatter.jp