Numeric and string literals

Number literals

Numeric literals used by the number data type are strings of digits, optionally starting with a minus sign and optionally with a decimal point anywhere in the string, but not before the minus sign, if present.


console.log(-.123 === -0.123); // logs: true
console.log(123 === 123.0); // logs: true

Leading zeros in the integer part are allowed in non-strict mode. However, if all digits after the leading 0 are smaller than 8, the number is interpreted as an octal number. In strict mode it will throw a SyntaxError.


console.log(083 === 83); // logs: true // In strict mode it throws a SyntaxError.
console.log(0123 === 83); // logs: true // In strict mode it throws a SyntaxError.

Decimal numeric literals can also follow the format for scientific e-notation. These literals are specified by a letter E or e, directly preceded by a number (as described above) and directly succeeded by an (optionally signed) integer.

1.23e7 = 1.23 × 107 = 12 300 000.


console.log(1.23e7 === 12300000); // logs: true
console.log(0e+100 === 0); // logs: true
console.log(1e-3 === .001); // logs: true
console.log(1e1 === 10); // logs: true
console.log(313e-2 === 3.13); // logs: true
console.log(1E3 === 1000); // logs: true

To improve readability, underscores can be used as numeric separators. Underscores are only allowed directly between two digits. See "can I use numeric separators?" for browser support.


console.log(1.23e7 === 12_300_000); // logs: true
console.log(12_345e-2 === 123.45); // logs: true
console.log(1_234.56 === 1234.56); // logs: true
console.log(0e1_000 === 0); // logs: true

Also binary, octal and hexadecimal integers can be represented by numeric literals. A leading zero followed by a lowercase or uppercase b is used for binary integer literals, a leading zero followed by a lowercase or uppercase o is used for octal integer literals, and a leading zero followed by a lowercase or uppercase x is used for hexadecimal integer literals. Digits after this prefix must be existing digits within the specific number system. Note that the decimal point and the scientific e-notation are only applicable within decimal numeric literals.


const binNum = 0b01100100;
console.log(binNum); // logs: 100
console.log(0b11111111); // logs: 255
console.log(0b0100_0000_0000 === 1_024); // logs: true // binary literal with its bits grouped per nibble (half-byte).
console.log(0o144); // logs: 100
console.log(0xA); // logs: 10
console.log(0X3E8); // logs: 1000
console.log(0x3e8); // logs: 1000
console.log(-0xff); // logs: -255

console.log(0b313); // logs: SyntaxError: missing binary digits after '0b'.
console.log(0o88); // logs: SyntaxError: missing octal digits after '0o'.

Many programming languages have more specific numeric data types, instead of one generic number type as in JavaScript. For example, they may differentiate between integers and "floating points". In JavaScript 2 strictly equals 2.0, but this is not the case in all programming languages.

BigInt literals

The numeric primitive BigInt uses the same literal format as described above, except with a lowercase letter n suffix, with no decimal point (it's an integer) and with no e-notation allowed. A BigInt literal cannot begin with a zero.


console.log(1234567891011121314151617181920n); // logs: 1234567891011121314151617181920n
console.log(9_007_199_254_740_992n); // logs: 9007199254740992n
console.log(-0b11111111111111111111111111111111111111111111111111111n); // logs: -9007199254740991n

See Floating points in JavaScript for more information on when BigInts can be useful.

String literals with quotation marks

String literals are texts enclosed in single or double quotes. Quotes within a string are part of the string and must be single quotes if the enclosing quotes are double and vice versa. It is also possible to use a backslash escape sequence (\" or \').


let namePers1 = "Jane",
    namePers2 = 'Jane';
console.log(namePers1 === namePers2); // logs: true

console.log("This is what they call a 'string', right?"); // logs: "This is what they call a 'string', right?"
console.log('This is what they call a "string", right?'); // logs: "This is what they call a "string", right?"
console.log("This is what they call a 'string', right?" ===
  'This is what they call a "string", right?'); // logs: false
	
console.log("This is what they call a \"string\", right?"); // logs: "This is what they call a "string", right?"
console.log('This is what they call a \'string\', right?'); // logs: "This is what they call a 'string', right?"	

For readability of the source code in the code editor, you may wish to specifically break a long string into multiple lines, without affecting the actual string contents. However, a string literal containing an unescaped newline character (by pressing the ↵ Enter key) throws a SyntaxError. To circumvent this, you can either use string concatenation to add a sequence of string literals by using the + operator, or use line continuation by using the backslash character (\). Make sure there is no space or any other character after the backslash.


console.log("This is line 1
and this is line 2");
  // logs: SyntaxError: "" string literal contains an unescaped line break

console.log("This is line 1 " +
"and this is line 2");
  // logs: "This is line 1 and this is line 2"

console.log("This is line 1 \
and this is line 2");
  // logs: "This is line 1 and this is line 2"

Template literals

Template literals (or template strings) are string literals allowing embedded expressions. Template literals are enclosed in grave accents, aka backticks (` `).


let namePers1 = "Jane",
    namePers2 = `Jane`;
console.log(namePers1 === namePers2); // logs: true

Template literals can contain placeholders (${expression}) to interpolate JS expressions or nest other templates. First an example of using normal string literals and string concatenation to add a sequence of string literals and variables together to form a single combined string:


let firstNumber = 2,
    secondNumber = 20;
console.log("The sum of "+ firstNumber +" and "+  secondNumber +
  " is "+ (firstNumber +  secondNumber) + ".");
  // logs: "The sum of 2 and 20 is 22."

Then using a template literal to get the same result:


let firstNumber = 2,
    secondNumber = 20;
console.log(`The sum of ${firstNumber} and ${ secondNumber} is ${firstNumber +  secondNumber}.`);
  // logs: "The sum of 2 and 20 is 22."

Also in a template literal you can use line continuation (by using the backslash character) to break long strings in the code editor. You can even use string concatenation (by using the + operator) for this purpose.


let firstNumber = 2,
    secondNumber = 20;
console.log(`The sum of ${firstNumber} and \
${ secondNumber} is ${firstNumber +  secondNumber}.`);
  // logs: "The sum of 2 and 20 is 22."

let firstNumber = 2,
    secondNumber = 20;
console.log(`The sum of ${firstNumber} and ` +
  `${ secondNumber} is ${firstNumber +  secondNumber}.`);
  // logs: "The sum of 2 and 20 is 22."

As opposed to a string literal, a template literal containing an unescaped newline character (by using the ↵ Enter key) actually returns a newline. A string or template literal can also include a single character escape sequences for a newline: \n.


console.log("This is line 1 \nand this is line 2");

console.log(`This is line 3 \nand this is line 4`);
   
console.log(`This is line 5
and this is line 6`);
Output to the console:

"This is line 1 
 and this is line 2"

"This is line 3
 and this is line 4"

"This is line 5
 and this is line 6"