What is decimal literal in JavaScript?
A decimal literal in JavaScript is a numeric literal that represents a base-10 (decimal) number, which is the most common way we represent numbers in everyday life. It consists of a series of digits (0-9) without any prefix. Decimal literals can be integers or floating-point numbers.
Types of Decimal Literals:
- Integer Decimal Literal: These are whole numbers without any fractional or decimal part.
Examples: 10
, 42
, -7
, 0
- Floating-point Decimal Literal: These are numbers that include a decimal point and may have a fractional part. You can also use floating-point notation for large or small numbers.
Examples: 3.14
, 0.5
, -2.718
, 1e6
(which represents 1 * 10^6
or 1000000
)
Examples of Decimal Literals:
Integer Decimal Literal:
let a = 10; // Decimal integer literal
let b = 42; // Another integer
let c = -5; // Negative integer
Floating-point Decimal Literal:
let d = 3.14; // Floating-point literal
let e = 0.5; // Another floating-point literal
let f = -0.75; // Negative floating-point literal
Exponential Notation (for large or small floating-point numbers):
let g = 1e6; // 1 * 10^6, or 1000000
let h = 2.5e-4; // 2.5 * 10^-4, or 0.00025
Characteristics:
- A decimal literal is the default numeric literal in JavaScript when you don't use prefixes like
0x
(for hexadecimal),0b
(for binary), or0o
(for octal). - Decimal literals support both integer and floating-point numbers.
Example Usage:
console.log(10); // Outputs: 10 (integer)
console.log(3.14); // Outputs: 3.14 (floating-point)
console.log(2e3); // Outputs: 2000 (exponential notation)
Decimal vs Other Numeral Systems:
- Decimal literals are base-10 numbers.
- JavaScript also supports other number systems like:
- Hexadecimal (base-16) with
0x
prefix. - Binary (base-2) with
0b
prefix. - Octal (base-8) with
0o
prefix.
- Hexadecimal (base-16) with
Now you should understand, decimal literals are the most commonly used numeric literals in JavaScript and represent numbers in base-10, either as integers or floating-point numbers.
Hope it helps.