Skip to main content

Javascript Numbers

The Numbers are a primitive data type that represents numeric values. JavaScript numbers can be written with or without a decimal point, and can also be represented using scientific notation.

Examples of JavaScript numbers:

let x = 3; // integer number
let y = 3.14; // floating-point number
let z = 2.99e8; // scientific notation

JavaScript also provides several built-in methods for working with numbers, such as

  • toFixed() for rounding a number to a specified number of decimal places,
  • toPrecision() for formatting a number with a specified number of significant digits, and
  • isNaN() for checking whether a value is not a number.

Some examples of using these methods:

let num = 3.14159;
let rounded = num.toFixed(2); // "3.14"
let formatted = num.toPrecision(3); // "3.14"
let isNotANumber = isNaN(num); // false

Adding Numbers and Strings

Adding numbers and strings can have different results depending on the data types involved.

When you add two numbers in JavaScript using the + operator, the result is a numerical value:

var a = 5;
var b = 7;
var c = a + b;
console.log(c); // Output: 12

When you add two strings using the + operator, the result is a concatenated string:

var a = "Hello";
var b = "World";
var c = a + b;
console.log(c); // Output: "HelloWorld"

However, when you add a number and a string using the + operator, JavaScript will convert the number to a string and concatenate the two values:

var a = 5;
var b = " apples";
var c = a + b;
console.log(c); // Output: "5 apples"

Alternatively, you can use string interpolation (template literals) to combine a number and a string:

var a = 5;
var b = "apples";
var c = `${a} ${b}`;
console.log(c); // Output: "5 apples"
caution

Adding values of different data types without conversion can lead to unexpected behavior/errors.

Numeric Strings

Numeric strings are strings that contain only numeric characters, but are still represented as strings rather than numerical values.

For example, "10" is a numeric string, but 10 is a number.

To convert a numeric string to a number in JavaScript, you can use the parseInt() or parseFloat() functions. The parseInt() function converts a string to an integer, while the parseFloat() function converts a string to a floating-point number.

var a = "10";
var b = "3.14";
var c = parseInt(a);
var d = parseFloat(b);
console.log(typeof(c), c); // Output: "number" 10
console.log(typeof(d), d); // Output: "number" 3.14

You can also perform arithmetic operations on numeric strings directly, but JavaScript will attempt to convert the string to a number before performing the operation:

var a = "5";
var b = "10";
var c = a + b;
console.log(c); // Output: "510"

In the above example:

  • JavaScript concatenates the two strings instead of performing addition because the + operator is used with two strings.
  • To add the two numeric strings together, you need to convert them to numbers first:
var a = "5";
var b = "10";
var c = parseInt(a) + parseInt(b);
console.log(c); // Output: 15
tip

When converting a numeric string to a number, ensure it only contains valid numeric characters. Invalid characters can cause conversion to fail or result in unexpected behavior.