There are five primitive types; Number, String, Boolean, null and undefined. Their values are stored directly in the variable for a given context. We can use typeof operator to identify primitive types with exception of null, which must be compared directly against the special value null.
color1 = "red"
color2 = color1
color2 = "green"
Inspect type of variables;
typeof(color1) //string
typeof(color2) //string
typeof(10) //number
typeof(true) //Boolean
typeof(undefined) //undefined
This is tricky value type.
typeof(null) //object
This has been acknowledge by TC39 as an error, the committee that design and maintain JavaScript.
The best way to determine if a value is null is to compare it against null directly;
value = null
value === null //true
value = false //false
value === null
“10” == 10 //true
“10” === 10 //false
When you use the double equals, the string “10” and the number 10 are considered equal because the double equals converts the string into a number before it makes the comparison. The triple equals operator doesn’t consider these values equal because they are two different types.
console.log(undefined == null) //true
console.log(undefined === null) //false
Null is a data type that has a single value null. When you’re trying to identify null, use triple equals so that you can correctly identify the type.
Strings, numbers and Booleans primitive types have methods. The null and undefined types have no methods. Strings, in particular have numerous methods.
var name = "Shahzad Khan"
var lowerName = name.toLowerCase() //convert to lowercase
var firstLetter = name.charAt(0) ////get first character
var middleOfName = name.substring(2,5) //get character 2-4
s.length //return number of characters
var count =10
var fixedCount = count.toFixed(2) //convert to “10.0”
var hexCount = count.toString(16) //convert to “a”
var flag = true;
var stringFlag = flag.toString() //convert to “true”
Primitive values are not objects though they have methods.
“Not a number” is used in cases where an arithmetic operation produces a result that is not a number.
a = 9/undefined //display NaN
typeof a //display number
Equality operator (>, <, ==, !=, >=, <=) return Boolean as their result.
var f = 10
var t = 20
f != t //return true
The undefined data type is returned when we access a property on an object that does not exists or use it before it is declared or before it is assigned a value;
typeof(z) //variable is not declared
There are some values in these types which evaluates to true and some evalues to false;
false, 0 (zero), “” (empty string), null, undefined, NaN (technically NaN is not true/false. It can only be detected with isNaN function)
To test this;
0 == false
All other values represent true values.
When evaluating the value of variables in conditional statement we do this;
var a = null
if (a == undefined || a == null)
{ a = 1;
}
It is possible to simple write
var a = null
if (!a) {
a = 10
}
Likewise, if a variable has a value, we can write something like;
If (a){
console.log(a)
}
This shortcut is useful and extensively used in JavaScript code.
JavaScript is a dynamically typed language. In a statically typed language, the compiler can perform type checking, if a variable is defined to store an integer. JavaScript variables derive their types based on the values they are assigned at run-time. Variables can change their type if they are assigned a new value.
As a result, it is not possible to perform static type checking in JavaScript.
Consider this example;
function add(v1, v2) {
return v1 + v2
}
If we invoke this function as;
add(1,1) //return 2
This is expected result. We can also invoke this function as;
add("1",1) //return 11
This is unexpected because JavaScript has performed string concatenation between number and the string. This is one of the reasons why the typeof operator is so important. It allows the function to be rewritten as;
function add(v1, v2) {
if (typeof v1 === "number" && typeof v2 === "number") {
return v1 + v2;
} else {
throw "both argumens must be number";
}
}
Now try to pass arguments;
add(“1”,1) //return “both arguments must be number
On a side note, using the “+” operator on different data types produces a variety of random results;
{} + [] //return 0
[] + {} //return object
Even using the “+” operator on the same data types produce meaningless results;
[] + [] //return empty string
{} + {} //return object array