Types in JavaScript
JavaScript just like any programming language has types, this is used to qualify what type of data that value is. This in itself can be useful when programming sometimes you would want to know what type a value is before an action is taken or otherwise if it is not. If you are confused it is like in the human world where there is categories for objects like furniture, clothing. You can take type to be category of a value but the value itself is like an item
There are 7 primitive types in JavaScript as at writing, they include:
- Number
- String
- Boolean
- Null
- Undefined
- Symbol
- BigInt
NOTE: Symbol and BigInt are the recent additions to the primitive data types.
Number
JavaScript and it’s peculiarity cannot be overstated, if you have worked with other programming languages you will notice it has an int(integer) and floating point(or what we know for decimal). JavaScript doesn’t have such so, both integer and floating point are treated in the Number type.
JavaScript represent each number type with 64 bits, in other words the numbers JavaScript can represent is limited, computers use the binary system, so 2⁶⁴ will give us over 18 quintillion (18 zeros or 18 billion billion) to be precise 18,446,744,073,709,551,616 that is a lot of values JavaScript can represent in a Number, in theory the maximum single number that JavaScript can represent is 2⁶⁴(0 — 18,446,744,073,709,551,615) don’t be confuse about the change in the last digit what 2⁶⁴ results in is the possible values not actual value and also computer start counting from zero. If you do not deal with astronomical number you are safe.
Not all number less than 18 quintillion can be represented in JavaScript. In the real sense JavaScript can only stored whole numbers in the range of 9 quadrillion (15 zero or 9 million billion) to be precise 9,007,199,254,740,991, because other values need to be represented like the position of the decimal point and negative sign which takes up bits.
Special Number
These are technically numbers, but they are not whole numbers or nonwhole number. Infinity and -Infinity fall in this category. If you have been coding for a while you must of seen NaN (everybody’s favorite), that also is a Number, funny right. You must have noticed when NaN rears its head is mostly in numeric operation that don’t yield meaningful results. e.g. Infinity-Infinity, 0/0, 0/5 all these make NaN come out of its dark hole.
String
How does computer recognize text? Wrap it in a, single quotes(‘’), double quotes (“”), backticks (``). This how a computer represent text, this makes the computer not mistake it for a keyword or variable. Almost any character can be enclosed in quotes and JavaScript will make a string out of it.
When enclosing with a text in a single quote or double quote, it will escape any line breaks. What this mean is when you are typing in a text editor, you may want to start on a newline, to start a paragraph or a sentence by pressing enter. JavaScript in its godly fashion will not be bother/appeased by your humanly way of requesting for a newline.
JavaScript in its infinite wisdom has made a way to request for a newline. What did it give us? ’ \n’, put this character in a string after any character and you will see your newline.
Don’t get confused by the letter ’n’, what is giving it the superpower that it has is the backlash (\), it gives meaning to some character thereby giving them the superpowers that they posses. So ’n’ is just a letter without the backslash, but with the backslash it has a different functionality.
Backslash (\) also has other uses apart from giving letter ’n’ superpower. It is used for also dialing down some characters superpowers. Take single quotes (‘’) and double quote (“”) for example JavaScript knows when if sees a single quote(‘’) or double(“”) it expects a string, but what if you just want to put words in just quote like quote someone or represent an apostrophe that will be impossible. Is it? Backslash to the rescue it , so instead of giving it superpowers it takes it away so a single quote(‘’) and double quote(“”) can do what quote do in the human world, be a quote simple as ABC right.
Single quotes and Double quotes can also be used in a string without the superpower-giving backslash.
Boolean:
This is useful when you have a value that has two possible outcome yes or no, on or off.
JavaScript gives you the ability to cater for such outcomes with it Boolean type, which are of two values, “yes” or “no” or “on” and “off” and are represented as such.
JavaScript has a Boolean type, which has two values, true and false, which are written as those words
SYMBOL
Symbol was introduces in ECMASCRIPT 2015. It creates a unique identifier when invoked, which is kept private for internal use. All that remains after the creation is the symbol reference. One of the use cases for using a symbol is to use it as a key in an object since it returns a unique identifier the key is said to be unique and thereby can’t be overridden by another script accessing that object.
It can be called with a name. It create a new unique value every time the symbol function is invoked, despite have the same symbol name. Find more about symbol here
BigInt
BigInt is a special numeric type that provides support for integers of abitrary length.
I wrote in the Number section that JavaScript represents a Number in 64 bits meaning. This gives JavaScript the ability to represent 18 quintillion integers but we have negative numbers as well as decimal number all this takes up bits. So the JavaScript can reliably represent with the Number primitive in integer over 9 quadrillion (15 zeros or 9 million billion).
The exact value of 9 million billion is 9007199254740991, so JavaScript can safely represent the value of an integer. This can be gotten by Number.MAX_SAFE_INTEGER constant.
This is useful if you want to do precise calculation or where large numbers are the norm. This likely not what you will use everyday, but it is there when you need it.
A BigInt can be created by add n to a Number (integer) or invoking the BigInt() function.
You can also make small numbers a BigInt.
Conclusion
These are the primitive data types of JavaScript, some might not be useful to you now. Keep in mind that they are there when you need them. JavaScript is a peculiar programming language, it has it on quirks and cool factor. Keep in mind to see some weirdness along the way.
Happy Coding!!!