Problem
People often encounter terms such as object, basic type (primitive), and literal value (literal), what is the difference between the three. How can we tell them apart?
Solution Solutions
A JavaScript literal represents a value of a particular type, for example, a quoted string (string), floating-point number, or Boolean value (Boolean):
"This is a string"
1.89
True
The JavaScript base type is an instance of a particular data type. There are 5 of these types in javascript: String, number, Boolean, NULL, and Undefind. Here are some examples of basic javascript types;
"This is a string"
Null
Of these basic data types, 3 have corresponding constructor object: String number and Boolran. These objects provide access to built-in properties and methods, allowing us to do things beyond simple assignment and sequential access.
var str1 = "This is a string"
Console.log (str1.length)//Access object's Length property
When declaring a variable, we are like manipulating a simple string or number.
var str1 = "This is a string"
However, we actually opened the door to inform a wide range of functions. If you are not relying on JavaScript objects, we can assign a string, a number, or a Boolean value to a variable. Then access it, however, if we are going to do more with the variable, we need to use the JavaScript object and its properties for that data type. For example, if you want to see the length of a string object, we can access the Long property
var str1 = "This is a string"
Console.log (Str1.length)
Behind the scenes. When the code accesses a String object property on a literal. A new string object is created, and its value is set to the value of the string contained in the variable. Access and print out the length property, and discard the newly created string object. In fact, JavaScript does have 5 data types: String, numeric, Boolean, NULL, and Undefind. Only string, numeric, and Boolean data types have corresponding constructor method objects. strings, floating-point numbers, integers, and Boolean values are literals:
var str1 = "This was a simple string"; //The string inside the quotation marks is the literal value
var num1 = 1.89; //1.89 is a literal value
var answer = true; True is a literal value
We can use a literal value to represent or use the object without the new operator, thereby creating a Boolean, string, and numeric variable of the base type:
var str1 = string ("This is a string"); //Basic type String
var num1 = number (1.89); Basic type Number
var bool1 = Boolean (true); //Basic Type Boolean value
To intentionally instantiate an object. With the new operator:
var str1 = new String ("This is a string");
var num1 = new number (1.88);
var bool1 = new Boolean (true);
When you use strict equality to compare an object example and a literal, you can quickly distinguish between a primitive type and an object instance.
var str1 = string ("string");
var num1 =number ("1.88);
var bool1 = Boolean (true);
if (str1== "string") {
Console.log (' equal ')
}
if (num1==1.88) {
Sonsole.log (' equal ')
}
if (bool1==true) {
Console.log (' equal ')
}
var str2 = new String ("string");
var num2 = new number (1.88);
var bool2 = new Boolean (true);
if (str2== "string") {
Console.log (' Not Equal ')
}
if (num2==1.88) {
Console.log (' Not Equal ')
}
if (bool2==true) {
Console.log (' Not Equal ')
}
The output results are as follows:
Equal
Equal
Equal
Not equal
Not equal
Not equal
The base type variable is strictly equal to the literal value, while the object instance does not. Why are basic type variables strictly equal to literal values? Because the base type is compared by value, the value is a literal value.
Most of the time, JavaScript developers don't create object instances directly for 3 basic data types, and developers just want a numeric variable, Boolean, or string variable that works like a number, Boolean, or String. Instead of having an object, we don't need the extension of the object, and more importantly, when the developer uses strict equality or type checking in the code, they want a variable that is consistent with the desired data type, rather than an "object":
var num1 = 1.88;
var num2 =new number (1.88);
Console.log (typeof num1); Output number
Console.log (typeof num2); Output Object
The difference between a JavaScript object, a base type, and a literal value