Data types basically specify what kind of data can be stored and manipulated within a program.
Statically typed means the type is enforced and won’t change so easily. All variables must be declared with a type.
int x = 5
string y = ‘abc’
It infer variable types at run-time. This means once your code is run the compiler/interpreter will see your variable and its value then decide what type it is.
var a = 1; // number
Any variable can be assigned (and re-assigned) values of all types.
var x; // Now x is undefined
x = 5; // Now x is a Number
x = “John”; // Now x is a String
Weakly typed languages allow types to be inferred as another type.
1 + '2' // '12' In JS it sees you’re trying to add a number with a string — an invalid operation — so it coerces your number into a string and results in the string ‘12’.
var x = 16 + “Volvo”; //16Volvo
var x = "Volvo" + 16; //Volvo16
var x = 16 + 4 + "Volvo"; //20Volvo
A primitive is not an object and has no methods of its own. All primitives are immutable (values, which are incapable of being changed). These seven types are considered to be primitives.
- Boolean -Booleans can only have two values:
typeof nullis an object. It should be
- Number -Numbers can be written with, or without decimals.
- BigInt -The
- String -Strings are written with quotes. You can use single or double quotes.
- Symbol -A Symbol is a unique and immutable primitive value and may be used as the key of an Object property
and Object -Everything else is in Object type.
There are two that are the main ones you’ll use for your own structures:
There are many other objects too just to list a few:
Notice some of these were on the primitive list too, but don’t confuse them. These act as constructors to create those types. Example:
Boolean('a') // true
Important Concepts that you should know
The typeof Operator
typeof operator returns the type of a variable or an expression.
Difference Between Undefined and Null
null are equal in value but different in type.
typeof undefined // undefined
typeof null // objectnull === undefined // false
null == undefined // true
Why is Null an Object?
If you see the above example, null
‘object’. According to documentation It should have returned null!.
What went wrong?
Read more about the rejected fix proposal here
Why NaN is a number?
typeof NaN; // number
NaN is the result of some mathematical operations that can’t be quantified as a number but
NaN can be defined as a numeric type.
A primitive is not an object and has no methods of its own
“You said primitives have no methods but then explain how
This was not a mistake and that comes from the documentation itself.
First, do not confuse constructors with primitives — every primitive has a constructor or parent object.
JS knows when you’re trying to access a method on a primitive and behind the scenes, it will use the constructor to make an object out of your primitive.
Once it runs the method that object is then garbage collected. (Removed from memory)
Thanks for reading! Leave any questions/feedback in the comments.