Javascript - Number

> (World Wide) Web - (W3|WWW) > Javascript (Js|ECMAScript) > ECMAScript - Grammar Standard

1 - About

Most programming languages have several types of numeric data, but JavaScript gets away with just one.

All numbers are and are stored as doubles.

See also': Javascript - Integer

Advertising

3 - Declaration

var myVariable = 10;
var myVariable = +10;
var myVariable = Number(10);

4 - Integer

There is no integer in a javascript.

var myNumber = 0.1 + 0.2 
 
console.log("myNumber is not 0.3 but " + myNumber)

4.1 - Coercion

4.1.1 - Explicit

To convert values to numbers explicitly, you can use:

  • the Number function
  • or the unary + operator:

Example:

  • Adding the + before the string “3” make it a number

console.log(typeof(+"3"))

Advertising

4.1.2 - Implicit

The arithmetic operators -, *, /, and % all attempt to coerce their arguments to numbers before doing their calculation.

The operator + is subtler, because it is overloaded to perform either numeric addition or string concatenation, depending on the types of its arguments.

Silent coercions can also hide errors because it will prevent errors or failure in an arithmetic calculation

4.1.3 - Value

Variable value Number Value
null 0
true 1
false 0
Non-Number String NaN
Empty String 0
Number String The number represented
Object without the valueOf property NaN
Object with the valueOf property The return value of the valueOf function
Empty Array 0
Non Empty Array with one number value The first number
Non Empty Array with more than one value NaN
undefined NaN
NaN NaN
function NaN

Proof:

x = [
    null, 
    true, 
    false, 
    "Non-Empty String",
    "",
    "1",
    { toString: function() { return "Object without valueof"; } },
    { toString: function() { return "Object with valueOf"; } , valueOf: function() { return 10; } }, 
    undefined, 
    [], 
    [ 1 ], 
    [ 1, 2 ], 
    NaN, 
    function(){} 
];
 
function numberCoerced(value) {
  this.value = value;
  this.coercedValue = Number(value);
  this.valueType = typeof(value);
}
 
numbers = [];
for (i in x) {
  numbers.push(new numberCoerced(x[i]));
}
console.table(numbers);

Advertising

4.2 - Bitwise

The bitwise arithmetic operators|bitwise arithmetic operators don't operate on their arguments directly as floating-point numbers, they implicitly convert them to 32-bit integers.

5 - Operators Arithmetic

5.1 - Integer and real number

Most operators works on integer and real number (float)

function operationArithmetic(desc,opString){
    this.descripion = desc;
    this.operation = opString;
    this.outcome = eval(opString);
}
 
var ops = [
    ["Multiplication","0.1 * 1.9"],
    ["Addition", "-99 + 100"],
    ["Minus", "21 - 12.3"],
    ["Division","2.5 / 5"],
    ["Modulo", "21 % 8"]
];
 
opsTable = [];
for (i in ops) {
  opsTable.push(new operationArithmetic(ops[i][0],ops[i][1]));
}
console.table(opsTable);

5.2 - String

console.log('"10" + 1 ='); // Concat
console.log("10" + 1); // Concat
 
console.log('1 + "10" = '); // Concat
console.log(1 + "10"); // Concat
 
console.log('1 + 2 + "10" performs an addition, then a concatenation giving the below result:');
console.log(1 + 2 + "10"); // Addition + Concat
 
console.log('true + "10" = '); // Concat
console.log(true + "10"); // Concat
 
console.log('1 + true = ');
console.log(1 + true); // True is coerced to 1

  • Minus

console.log("10" - 1); // Arithmetic Operation
console.log(10 - "1"); // Arithmetic Operation

6 - Representation in different base

6.1 - Decimal To base

  • The number 8 in 32-bit integer in binary

console.log(    (8).toString(2)    ); // 8 in base 2 (binary)
 
// 1000
// Exactly 00000000000000000000000000001000 in 32 bit but the 0 at the left are not shown.

where:

  • The argument to toString specifies the radix

6.2 - Base to decimal

console.log("The number 1001 in base 2 has the representation ("+parseInt("1001", 2 )+") in base 10") 
// 1001 is a base 2 (binary) number representation

6.3 - Example

function numberRepresentation(decimal, binary, hexadecimal) {
  this.decimal = decimal;
  this.binary = binary;
  this.hexadecimal = hexadecimal;
}
numbers =[]
for (var i=0;i<=16;i++) {
   numbers.push(
       new numberRepresentation(
           i,
           i.toString(2),
           i.toString(16).toUpperCase()
       )
   );
}
console.table(numbers);

web/javascript/es/number.txt · Last modified: 2019/02/16 17:40 by gerardnico