JavaScript type

The latest ECMAScript standard defines 1 object data type and 7 primitive data types.

The 7 primitive data types are:

  1. Boolean. true and false.
  2. null. A special keyword denoting a null value. (Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.)
  3. undefined. A top-level property whose value is not defined.
  4. Number. An integer or floating point number. For example: 42 or 3.14159.
  5. BigInt. An integer with arbitrary precision. For example: 9007199254740992n.
  6. String. A sequence of characters that represent a text value. For example: “Howdy”
  7. Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.

ECMAScript has two built-in numeric types: Number and BigInt.

Note:

All primitives (the values themselves not the variables) are immutable. A variable can be reassigned a primitive value, but the existing primitive value can not be changed like an object.

validate

https://dmitripavlutin.com/javascript-defined-variable-checking/

// Examples of defined variables:

const pi = 3.14; // pi is defined
let result;      // result is defined

window.message = 'Hello';
message;         // message is defined

Find whether a variable is defined or not.

The typeof operator determines the variable’s type. typeof myVar can evaluate to one of the values:

  • 'boolean'

  • 'number'

  • 'string'

  • 'symbol'

  • 'object'

  • 'function'

  • 'undefined'.

// missingVar is not defined

typeof missingVar; // Doesn't throw ReferenceError

missingVar;        // Throws ReferenceError

942

https://stackoverflow.com/questions/5113374/javascript-check-if-variable-exists-is-defined-initialized

The typeof operator will check if the variable is really undefined.

if (typeof variable === 'undefined') {
    // variable is undefined
}

The typeof operator, unlike the other operators, doesn’t throw a ReferenceError exception when used with an undeclared variable.

However, do note that typeof null will return "object". We have to be careful to avoid the mistake of initializing a variable to null. To be safe, this is what we could use instead:

if (typeof variable === 'undefined' || variable === null) {
    // variable is undefined or null
}

Boolean

A Boolean is a logical data type that can have only the values true or false.

Truthy value and Falsy value:

  • JavaScript uses type conversion to coerce any value to a Boolean in Boolean contexts (like conditionals and loops).
  • All values are truthy unless they are defined as falsy which are listed below.

All the values considered to be false

All the values that considered to be false when encountered in a Boolean context like if condition (See Falsy value):

  • false The keyword false.
  • 0 The Number zero (so, also 0.0, etc., and 0x0).
  • -0 The Number negative zero (so, also -0.0, etc., and -0x0).
  • 0n The BigInt zero (so, also 0x0n). Note that there is no BigInt negative zero — the negation of 0n is 0n.
  • "", '' , “` ` Empty string value.
  • null null
  • undefined undefined
  • NaN NaN
  • document.all Objects are falsy if and only if they have the [[IsHTMLDDA]] internal slot.That slot only exists in document.all and cannot be set using JavaScript.

The logical AND && operator

Note the logical AND (&&) returns the value of the first falsy operand encountered when evaluating from left to right, or the value of the last operand if they are all truthy.

Examples

console.log(1 && 0 && 'abc')     // 0
console.log(false && 1)          // false
console.log(true && 'abc')       // 'abc'

Convert a Boolean to an Boolean object

The Boolean object is an object wrapper for a boolean value.

Examples

var x = new Boolean(false);
if (x) { // x is an object not a boolean primitive
  // this code is executed
}

x = false
if (x) {
  // this code is not executed
}

// Do not use a Boolean object to convert a non-boolean value to a boolean value.
x = Boolean(expression);     // use this...
x = !!(expression);          // ...or this
x = new Boolean(expression); // don't use this!

Boolean object:

The value passed as the first parameter is converted to a boolean value, if necessary. If the value is omitted or is 0, -0, null, false, NaN, undefined, or the empty string (""), the object has an initial value of false. All other values, including any object, an empty array ([]), or the string “false“, create an object with an initial value of true.

Number

Number type:

The Number type is a double-precision 64-bit binary format IEEE 754 value (numbers between -(2^53 − 1) and 2^53 − 1). In addition to representing floating-point numbers, the number type has three symbolic values: +Infinity, -Infinity, and NaN (“Not a Number”).

To check for the largest available value or smallest available value within ±Infinity, you can use the constants Number.MAX_VALUE or Number.MIN_VALUE.

Examples

// 0 is represented as both -0 and +0. (0 is an alias for +0.)
console.log(+0 === -0);    // true

+Infinity, -Infinity, and NaN

// Infinity and -Infinity
console.log(42 / +0); // Infinity
console.log(42 / -0); // -Infinity

// NaN
console.log(NaN); // NaN

// Use isNaN() to determineswhether a value is NaN or not.
console.log(isNaN('100F'));      // true
console.log(isNaN(1.23));        // false
console.log(isNaN('3.14'));      // false
console.log(isNaN('0.0314E+2')); // false

Starting with ECMAScript 2015, you are also able to check if a number is in the double-precision floating-point number range.

// Check if a number is in the double-precision floating-point number range.

// Out of safe range, the precision may be lost.
console.log(Number.isSafeInteger(Math.pow(2, 53))); // false
// In safe range, the precision is safe.
console.log(Number.isSafeInteger(Math.pow(2, 53) - 1)); // true
// The max safe inteer and the min safe integer.
console.log(Number.MAX_SAFE_INTEGER); // 9007199254740991
console.log(Number.MIN_SAFE_INTEGER); // -9007199254740991

Resource