null
undefined
boolean
number
string
object
symbol
(added in ES6)typeof
returns one of the above types
typeof true === 'boolean' // true
typeof true === "boolean"; // true
typeof 42 === "number"; // true
typeof "42" === "string"; // true
typeof { life: 42 } === "object"; // true
typeof Symbol() === "symbol"; // true
But typeof have some special behaviour with null
and function
typeof null === 'object'; // true
typeof function a() {} === 'function'; // true
In JavaScript, variables don’t have types - values have types. Variables can hold any value, at any time.
undefined
is a value that a declared variable can hold. “Undeclared” means a variable has never been declared. To safely check for undeclared variables use:
typeof a !== 'undefined'
null
and undefined
undefined
hasn’t had a value yetnull
had a value and doesn’t anymoreJavaScript has just one numeric type: number. This type includes both “integer” values and fractional decimal numbers.
The best way to compare two floats
function numbersCloseEnoughToEqual(n1,n2) {
return Math.abs( n1 - n2 ) < Number.EPSILON;
}
Because of how numbers are represented, there is a range of “safe” values for the whole number “integers”. Numbers that are out of that range are represented as floats.
To test if variable is integer use
Number.isInteger( 42 ); // true
Number.isInteger( 42.000 ); // true
Number.isInteger( 42.3 ); // false
To test if variable is “save” integer
Number.isSafeInteger( Number.MAX_SAFE_INTEGER ); // true
Number.isSafeInteger( Math.pow( 2, 53 ) ); // false
Number.isSafeInteger( Math.pow( 2, 53 ) - 1 ); // true
There are some operations (like the bitwise operators) which are only defined for 32-bit numbers.
To check if variable is NaN
use
const a = 2 / "foo";
Number.isNaN(a); // true
It’s commonly a bad idea to pass Number
wrapper to function, and try to edit it value. Number
is mutable object and it can hold only one value.
function foo(x) {
x = x + 1;
x; // 3
}
var a = 2;
var b = new Number( a ); // or equivalently `Object(a)`
foo( b );
console.log( b ); // 2, not 3
String()
Number()
Boolean()
Array()
Object()
Function()
RegExp()
Date()
Error()
Symbol()
– added in ES6!If you call "my string".length
the string value is automatically boxed to object String
. The same for number
, boolean
.
Function.prototype
it is an empty function, RegExp.prototype
it is an empty regex, Array.prototype
it is an empty array. So it’s usefull to use that objects as default value. But you should be sure that these value will not be modified.
function isThisCool(vals,fn,rx) {
vals = vals || Array.prototype;
fn = fn || Function.prototype;
rx = rx || RegExp.prototype;
return rx.test(
vals.map( fn ).join( "" )
);
}
Converting a value from one type to another is often called “type casting,” when done explicitly, and coercion when done implicitly (forced by the rules of how a value is used).
JavaScript coercions always result in one of the scalar primitive values, like string
, number
, or boolean
.
toBoolean
undefined
null
false
+0
, -0
and NaN
""
document.all
(it is obsolete browser behaviour)Everything which is not from the above list evaluates to true
var a = new Boolean( false ); // true
var b = new Number( 0 ); // true
var c = new String( "" ); // true
toString
is used in case when we convert object to String
const obj = {
foo: 'bar',
toString: function () {
return 'the number 42';
}
}
console.log(obj); // 'the number 42'
valueOf
- is used in case when we convert object to Number
const obj = {
foo: 'bar',
valueOf: function () {
return 28;
}
}
obj + 14 // 42
toJSON
is called when object is passed to JSON.stringify
. toJSON
should return object which could be safely pass to JSON.stringify
var o = { };
var a = {
b: 42,
c: o,
d: function(){}
};
// create a circular reference inside `a`
o.e = a;
// would throw an error on the circular reference
// JSON.stringify( a );
// define a custom JSON value serialization
a.toJSON = function() {
// only include the `b` property for serialization
return { b: this.b };
};
JSON.stringify( a ); // "{"b":42}"