A computer works on a set of given instructions; it cannot differentiate between a number and a character. For example, if you write some numbers, such as 12345
, and some character, abcsd
, it cannot tell integers from characters. So, data types are used for this. The datatype tells which type of data is being used or referenced in a statement.
In all programming languages (C, C++, Java, JavaScript), data types are used. Every data type has a specific function of storing data. Many classical computer programming languages require you to specify the data type when you declare a data object. JavaScript does not have this requirement. Similarly, some databases require declaration of the data types for storing data.
JavaScript is a dynamic language. This means that if you declare an object, then its data type can be changed dynamically, for example:
var a=16; a="abc"; a=true;
In the first statement, the data type is an integer; in the second statement, the data type is a character; and in third statement, the data type is a Boolean. So, in JavaScript, the data type dynamically changes while the program is being processed.
There are six major data types defined in ECMAScript standard.
Null
Undefined
Boolean
String
Symbol
Number
Here, Null
and undefined
are trivial data types in JavaScript because each of these defines single values. JavaScript also supports a composite data type, which is known as the object. Objects in JavaScript are treated as an associative array.
Functions in JavaScript are also treated as objects. So, functions can also be stored as a variable.
Operators are used to find the type of a JavaScript variable. For example, you have two numbers variables, as follows:
5+1=6
Here, 5
and 1
are
operands and +
is an operator.
The syntax of this operator is as follows:
Typeof(operand)
The Typeof
operator is a unary operator; it returns a string that indicates the typeof expressions. The typeof
operator returns information as a string. There are six possible values that can be returned as a string. They are string
, number
, object
, function
, Boolean
, and undefined
.
Here are some examples of the typeof
operator:
typeof("5"+"1")
: This will return a stringtypeof(5+1)
: This will return a numbertypeof(5+"1")
: This will return a stringtypeof(5*"1")
: This will return a numberIn JavaScript, an empty variable or a variable without values has an undefined value. And, typeof
is also undefined. If you want to empty a variable, you can assign it an undefined value; for example:
var employee = undefined;
In JavaScript, undefined
is a property of the global
object. This means that the value is undefined and we declare it in the global scope of the browser.
There are basically three property
attributes of undefined
:
Writeable
Enumerable
Configurable
These three property
attributes show that, for modern browsers, these properties are not writeable, enumerable, and configurable. And, we should avoid overriding these properties. The undefined
type is a primitive type in JavaScript.
Undefined is a built-in type in the JavaScript library. Every type of function for which the value is not defined is consider as an undefined
type. For example, if you have a function without the return
statement, the result of that function will be considered as undefined.
Being a property of a global object, its value is initially undefined. We can access it as a global variable, although it's a global property. A simple idea of an undefined type is when a variable is not defined in your script, but it does exist and you are considering it as a global variable.
Whenever a function executes in a script, and it finishes executing without returning a value, then it returns the undefined
type, for example:
var abc, f1=function(){} f2=function() { var hello; return hello; } typeof(abc) ; // undefined typeof(f1); // function typeof(f2); // function
Here, in this example, the variable abc
is defined but no value has been assigned to it. Notice that undefined
type is a primitive property of a object declared in a global scope. Here, defining it as a global object does not mean that it cannot be redefined. It can be redefined according to ECMAScript. So, undefined
is not a reserved word; it can be used as a variable name in other scopes outside of its global scope.
When you use comparison operators, you can also declare the undefined
type there to know whether a variable has a value or not.
In JavaScript, null
shows an empty value.
varabc; // Here abc does not exist and it has been not initialized varabc=null; // Here abc exist but it does not has a value or a type
The data type of null
is an object in JavaScript. Null
shows that value does not exist in the script. When you want to empty a value, you declare it as a null
. Null
can be expected in places where we are expecting some values, but we find nothing.
There is a big difference between the null
and undefined
type. The data type of null
is an object and the data type of undefined
is undefined. In the identity
operator, null
cannot be undefined, but in the equality
operator, null
can be undefined. This is because the equality
operator will cast the value on the left to the same type before making a comparison.
Undefined means a variable is declared but values is not assigned. For instance, var abc
:
console.log(abc); //undefined console.log(typeof abc); //undefinednull is an empty value. It can be assigned to a variable which means variable is defined and an empty value has been assigned to it. For instance,var abc = null; console.log(abc); //null console.log(typeof abc); //returns object
Consider the following example:
Null===undefined; //It's a false because identity operator always verify data type //equality Null==undefined; //It's true because of equality operator
"You may wonder why the typeof operator returns object for a value that is null. This was actually an error in the original JavaScript implementation that was then copied in ECMAScript. Today, it is rationalized that null is considered a placeholder for an object, even though, technically, it is a primitive value."
- Professional JS For Web Developers, Wrox Press
There is only one type of number in JavaScript. Numbers can be written with or without decimals. Much larger or smaller numbers can be written with scientific notation.
Unlike other programming languages where we have several numeric data types like integer, float, double, and so on. JavaScript has a Number data type for all numeric values. JavaScript has three symbolic values for this type: infinity, and NaN (not a valid number). NaN defined in the number type in JavaScript shows that it is a reserved word, and its value is not a number in reality.
Integer values can be positive numbers, 0, or negative numbers. They can be of any base number like base 10, 16, or 8. But mostly, in JavaScript, numbers are written to base 10. To check the largest or smallest number representable or available in JavaScript, you can use constants of maximum and minimum values:
Number.maxValue Number.minValue
In the number type, you only check a number that has two representations, for example, if you have the number 0, then it has two representations—one is -0 and the other is +0. JavaScript numbers can be written with or without a decimal point; for example:
A non- decimal number var a=10 A number with decimal var b=10.0123
If the number is too large or too short, then we use a scientific notation's exponent for it. Consider the following example:
If we have a number 100000000 var a=1000e5 And 0.00111 var b=111e-5
The limit is 1e+21
(the console displays a scientific notation from the power of 10).
In programming languages, the Boolean
type works on the Boolean logic in which variables return results as true
or false
. These true
and false
are basically literals. The Boolean logic is used usually in comparison operators, for example, lesser or greater than, and so on.
Any variable can be assigned a Boolean
value written as true
or false
in lower case.
In JavaScript, when you declare a Boolean
data type to a variable, then the variable should not have any quote. Here is how you should use boolean assignment: var abc = "true"; //string assignment
and var abc = true; //boolean assignment
. The Boolean
variables are also used in the if
, if
then else
, and while
loop.
Here is an example showing how Boolean variables are used:
var x=true; if(x==true) { document.write("this true"); }
The Boolean
operators are also used when there is a lack of or presence of a value. For example, if we have a checkbox in our script, then its value is present or not depending on whether it is checked or not.
In other data types, there is an unlimited number of values a variable can take, but for the Boolean
data types, there are only two—true
and false
. This data type is usually used when we have a controlled structure such as a comparison, the if else
loop, and the while
loop. These are controlled structures. The If
and else
statement performs one action if the value is
true
; otherwise, it will perform another action, that is, false
.
You can also use the Boolean
data type as a comparative expression. A comparative expression is an expression that evaluates 0
, null
, and undefined
as false
and other values as true
. Consider the following example:
C=A+B
In this example, the result of A+B
is stored in C
first and then this expression is evaluated. If you want to check that the C
value is equal to A+B
, then you must use the comparison operator for this, as follows:
C==A+B
In JavaScript, anything that is written between quotes is treated as a string. Double or single quotes can be used.
Strings literals can be defined in two ways. One way is to write a string in double quotes and the other way is to write a string in single quotes.
In JavaScript, the text data is represented in the form of the string
data type. In programming languages, string is a combination of characters and numbers. A JavaScript string is an ordered sequence of zero or more characters.
The restriction for writing these strings is that you must use the same quotation mark at the beginning and end. This means that a string that starts with a single quote must end with a single quote, and the same rule applies for double quotation marks. Consider the following example:
var x="xyz" //double quotes var y='xyz' //single quotes var z="" //empty string
If there is a problem in a string and it cannot show some characters. For a representation of these characters, there is a technique in JavaScript—we define the escape sequence for this. This means that the character which we cannot write directly is written using an escape sequence. For example, if we want to insert a quotation mark within a string, we can do it by preceding quotation mark with a backslash. It is known as escaping the quotation mark. Similarly, string literals are unable to represent nonprinting characters such as tab, new line, and so on.
An escape sequence is a combination of characters starting with a backslash. Some escape sequences are as follows:
to new line.Here is an example of this:
var abc = "hello Morning"
Its output will be as follows:
Hello Morning
Object is a sequence of primitive and, sometimes, reference data types stored as name-value pairs.
The syntax is as follows:
varsomeObj= {property:value};
In JavaScript, objects are written into curly brackets. Each item of an object is called a property. Property values can be defined as any valid data type as well as functions or methods. Consider the following example:
varobj= {book_name:"xyz", Author:"abc"};
We can say that the object is a list of items, and each item in the list has some method and function stored as its name-value pair:
In JavaScript, everything is like an object, for example:
The property name of any item can be a string or number. When a property is a number, then the way of writing this property is different; we will write it as follows:
var age = {"30": "abc"};
In JavaScript, objects are used to store data and for writing your own custom methods and functions. Primitive values are, for example, null
, undefined
, true
, and false else
.
Object properties are basically variables that are used in methods and function internally. They can also be global variables for the whole page. There is a difference between a method and a function. A function is a standalone unit of an object, but a method is attached to an object and it is used with this
keyword:
Document.write ("hello world");
18.191.60.249