Primitive Type Variables
The Basic JavaScript Building Blocks. JavaScript has 7 primitive data types:
- Numbers
- Unlike many other programming languages, JavaScript does not define different types of numbers, like integers, short, long, floating-point etc. JavaScript numbers are always stored as double precision floating point numbers, following the international IEEE 754 standard. This format stores numbers in 64 bits, where the number (the fraction) is stored in bits 0 to 51, the exponent in bits 52 to 62, and the sign in bit 63:
- Modulus (%)
- The modulus operator (%) returns the division remainder.
- x = 5, y = 2 : z = x % y : z = 1 (remainder of 5 divided by 2)
- *note: The "MOD" operator with 2 is a good way to find out if any number is ODD or EVEN.
- if the mod operation returns 1, the number is ODD | 13 % 2 = 1 | 13 is an Odd number.
- if the mod operation returns 0, the number is EVEN | 14 % 2 = 0 | 14 is an Even number.
- The modulus operator (%) returns the division remainder.
- Incrementing (++)
- The increment operator (++) increments numbers.
- x = 5 : y = x ++ : y = 6 (5 incremented by 1)
- The increment operator (++) increments numbers.
- Decrementing (--)
- The decrement operator (--) decrements numbers.
- x = 5 : y = x -- : y = 4 (5 decremented by 1)
- The decrement operator (--) decrements numbers.
- Shortcuts (+= -= *= /=)
- Add, Subtract, Multiply, Divide by more than One at a time. Shortcut for operations.
- x = 5 || x += 5 is the shorcut version for x = x + 5
- This also works for -= | *= | /=
- Add, Subtract, Multiply, Divide by more than One at a time. Shortcut for operations.
- Powers **
- The power operator (**) returns the power of a number.
- x = 2 | y = 4 -- : 2 ** 4 = 16 : 2 to the 4th power returns 16 (2 * 2 * 2 * 2)
- The power operator (**) returns the power of a number.
- NaN: Not a Number
- In JavaScript, NaN is a number that is not a legal number. Any mathematical operation involving NaN, returns NaN. There are five different types of operations that return NaN:
- - Failed number conversion such as (e.g. 0 / 0)
- - Math operation where the result is not a real number (e.g. Math.sqrt(-1))
- - Indeterminate form (e.g. 0 * Infinity, 1 ** Infinity, Infinity / Infinity, Infinity - Infinity)
- - A method or expression whose operand is or gets coerced to NaN (e.g. 7 ** NaN, 7 * "blabla") — this means NaN is contagious
- - Other cases where an invalid value is to be represented as a number (e.g. an invalid Date new Date("blabla").getTime(), "".charCodeAt(1))
- In JavaScript, NaN is a number that is not a legal number. Any mathematical operation involving NaN, returns NaN. There are five different types of operations that return NaN:
- String
- Strings are for storing text. A JavaScript string is zero or more characters written inside quotes. You can use single or double quotes. You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
- Boolean
- A JavaScript Boolean represents one of two values: True or False. Boolean(10 > 9) evaluates to True because 10 IS greater than 9. Boolean(10 > 11) evaluates to False because 10 IS NOT greater than 11.
- Null
- - The null keyword represents an intentionally defined absence of value. null is a primitive, although the typeof operator returns that null is an object. You might define a variable as null with the expectation that it reflects either a value assigned to it at some point in a script or an explicitly absent value. You can also assign the null value to an existing reference to clear a previous value.
- - The intentional absence of any value
- - Null MUST be assigned
- Undefined
- - A primitive value assigned to variables that have just been declared, or to the resulting value of an operation that doesn't return a meaningful value. For example, this can happen when you declare a function in a browser's developer console. A function explicitly returns undefined when its return statement returns no value.
- - Variables that do NOT have an assigned value. (a variable that has been declared, but not assigned).
- Bigint
- JavaScript BigInt variables are used to store big integer values that are too big to be represented by a normal JavaScript Number. JavaScript integers are only accurate up to 15 digits. To create a BigInt, append n to the end of an integer or call BigInt()
- Symbol
- unknown at this time