Featureintro
Literals
Also called literal, array literal, object literal, numeric literal, string literal, regex literal, BigInt literal, boolean literal, null literal.
Literals are values that you write directly in the source: null, true, 42, 0xff, 10n, 'text', [1, 2], {a: 1} and /ab+c/. Array and object literals make a fresh object each time they run.
- Language
- JavaScript
Synopsis
| Kind | Examples |
|---|---|
| null | null |
| Boolean | true, false |
| number | 117, 0o17, 0xff, 0b101, 3.14, .5, 1e-3 |
| BigInt | 123n, 0x1fn |
| string | 'single', "double", `template ${x}` |
| array | [], [1, 2, 3], [1, , 3] |
| object | {}, { a: 1, 'b-c': 2 } |
| regular expression | /ab+c/g |
MDN describes literals as fixed values that you write in the script, not variables.1 In the grammar, the Literal production covers only NullLiteral, BooleanLiteral, NumericLiteral and StringLiteral.2 Array, object, regular expression and template literals have their own sections in 13.2.2
console.log(typeof null, typeof true, typeof 0xff, typeof 10n, typeof "a");
console.log(Array.isArray([1, 2]), typeof { a: 1 }, /ab+c/ instanceof RegExp);
console.log(0o17, 0xff, 0b101, 1e-3, 0x1fn);
object boolean number bigint string
true object true
15 255 5 0.001 31n
Semantics
r1. Number literals. A literal can be decimal, octal (0o), hexadecimal (0x) or binary (0b). A floating-point literal is decimal and "must have at least one digit, and either a decimal point or e (or E)".1 Example: synopsis.
r2. No sign in a number literal. MDN notes that "the language specification requires numeric literals to be unsigned". In -123.4, the - is a unary operator.1
r3. BigInt literals. "A trailing n suffix on an integer literal indicates a BigInt literal." Any base is OK, but "leading-zero octal syntax like 0123n is not allowed".1 Example: bigint-octal.
r4. String literals. A string literal uses double or single quotes. "A string must be delimited by quotation marks of the same type".1 Template literals use backticks. See Template literals.
r5. Array literals. "An array literal creates a new array object every time the literal is evaluated."1 "If you put two commas in a row in an array literal, the array leaves an empty slot for the unspecified element." A trailing comma is ignored.1 Example: fresh.
r6. Object literals. An object literal holds pairs of property names and values in braces. A name that is not a valid identifier or number "must be enclosed in quotes".1 Example: fresh.
r7. Regular expression literals. A regular expression literal is a pattern between slashes, such as /ab+c/.1
Examples
The examples run with Node.js 24.
Each evaluation makes a fresh array and object (rules r5 and r6):
function make() {
return { list: [1, , 3,], "odd-key": true };
}
const a = make();
const b = make();
console.log(a === b, a.list === b.list);
console.log(a.list, a.list.length, 1 in a.list, a["odd-key"]);
false false
[ 1, <1 empty item>, 3 ] 3 false true
The unary minus is not part of the literal (rule r2):
const x = -0xff;
console.log(x, -(0xff) === x, - 1 === -1);
-255 true true
A BigInt literal cannot use a leading zero for octal (rule r3):
This code does not compile, on purpose.
console.log(0o123n);
console.log(0123n);
Pitfalls
An object literal at the start of a statement
MDN says "Do not use an object literal at the beginning of a statement!" There, "the { will be interpreted as the beginning of a block."1 Wrap the literal in parentheses, as in ({ a: 1, b: 2 }).
This code does not compile, on purpose.
{ a: 1, b: 2 }
Holes are not undefined
[1, , 3] has an empty slot at index 1. "When using array-traversing methods like Array.prototype.map, empty slots are skipped."1 See Sparse arrays.
Comparison
| Topic | JavaScript | Python |
|---|---|---|
| kinds | null, Boolean, number, string; arrays, objects and regular expressions have literal syntax too2 | "string, bytes and numeric literals"; None, True and False are keywords3 |
| sign | not part of the literal1 | "Numeric literals do not include a sign"3 |
| list syntax | array literal [1, 2]1 |
"a possibly empty series of expressions enclosed in square brackets"4 |
| map syntax | object literal { a: 1 }1 |
dictionary display {1: 'one'}4 |
| complex numbers | none | "no complex literals", but imaginary literals such as 4.2j3 |
Python says "Literals are notations for constant values of some built-in types."3 Python calls [1, 2] a list display, not a literal.4 See Numbers.
References
- Grammar and types, MDN, Literals
- ECMAScript 2026 Language Specification, 13.2.3 Literals; 13.2.4 Array Initializer; 13.2.5 Object Initializer; 13.2.7 Regular Expression Literals; 13.2.8 Template Literals
- Lexical analysis, The Python Language Reference, 2.4 Literals; 2.6 Numeric literals
- Expressions, The Python Language Reference, 6.2.5.1 List displays; 6.2.5.4 Dictionary displays
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W3 standard |
|
Tag XQBBKB Edit this page History Report an error Markdown