JAVASCRIPT - DATA TYPES
We have seen a variable is used to hold a value. Be it a number or a name or even a sentence.
But how will JavaScript come to know, if the value is a number or a name?
Well! Once again, that's the beauty of JavaScript. JavaScript makes an intelligent guess, if the value is a number or a name or a sentence or something else and create the variable accordingly.
But! Just think for a moment. JavaScript must be using some mechanism by which it can determine if the value assigned to a variable is a number or a name or a sentence or something else.
And thus Data Types comes into picture.
Note : In other languages, you have to know all the Data Types to declare a variable. But in JavaScript, it is not necessary for you to know the Data Types. You can just assign the values and JavaScript will do the task of determining the Data Types.
Data Types
Data Types are the hidden gem that is used by JavaScript to determine if the variable is going to hold a number or a name.
But why does JavaScript needs to determine if the variable is going to hold a number or a name? Can it not create one type of variable and store all the values there.
It cannot do that. Because if you want to store a number. It would take less space.
On the other hand, if you want to store a String like Hello World. It would take a larger space.
So, whenever you want to create assign a String to a variable.
JavaScript makes an intelligent guess and finds out, it is a String type value and creates the storage space accordingly.
The above Data Type for String is called string Data Type.
And similarly for a number, for a decimal value e.t.c. JavaScript has different Data Types.
List of all Data Types used in JavaScript
-
String
As we have seen in the above example, a String is a set of letters/alphabets.
string Data Type
The Data Type for String is string.
When JavaScript finds data inside Double Quotes ("") of Single Quotes (''). It creates a String type variable for that value.
Or
A string variable can alse be declared using new String(), with the string inside the bracket ().
So, the Data Type of a variable is determined by JavaScript and is hidden from us.
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var x = "Hello World"
document.write(x)
</script>
</body>
</html>
Output :
Hello World
-
Numbers
Numbers could be a floating point number (i.e. 5.3322) or a whole number (i.e. 7 or 5).
The data type for Number is number. A number variable can be declared by assigning value to a variable directly.
var x = 5
A Number variable can alse be declared using new Number(), with the number inside the
bracket ().
Let us see them below:
Whole Number
As we have discussed above, the Data Type for a whole number is number.
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var x = 5
document.write(x)
</script>
</body>
</html>
Output :
5
Floating point number
The Data Type for a floating point number is also number.
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var x = 5.987
document.write(x)
</script>
</body>
</html>
Output :
5.987
-
Arrays
So far we have seen, how to store a Name, a sentence or numbers in separate variables. But what if, we want to store a name and a number in the same variable?
Well! Array is the Solution. Array, is going to store multiple values in one variable.
Array
An Array is a type of Collection that holds multiple values.
The declaration of an Array is super easy. You can place the multiple values inside square brackets [] and JavaScript will understand that it is an array.
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var x = [5, "John", "JavaScript"]
document.write(x)
</script>
</body>
</html>
Output :
5,John,JavaScript
An Array can also be declared using new Array(), with the elements inside parenthesis ().
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var x = new Array(5, "John", "JavaScript")
document.write(x)
</script>
</body>
</html>
Output :
5,John,JavaScript
-
Booleans
Boolean represents just two values, i.e. true or false.
boolean Data Type
A boolean Data Type can accept only true or false.
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var x = new Boolean(true)
var y = false
document.write(x)
document.write(y)
</script>
</body>
</html>
Output :
true false
Just remember all the letters of true and false should be in lower case.
-
Objects
We will be discussing more about objects in the coming tutorials. For now, let's understand in brief.
We have seen that an Array is capable of holding some values. Now, what if, we want the details of a student to be stored.
Say, a student can have a name, a roll number and age. And we wanted to store it in a variable in the below way:
name : John
roll : 38
age : 12
And this is where Object data type comes into picture.
Declaring an Object is super simple. Just put the above values inside {}. And the values should be separated by ':'.
var student = {name : John, roll : 38, age : 12}
And we are done.
Example :
<html>
<body>
<script language = "javascript" type = "text/javascript">
var student = {name : "John", roll : 38, age : 12}
document.write(student.name, ", ")
document.write(student.roll, ", ")
document.write(student.age)
</script>
</body>
</html>
Output :
John, 38, 12
So, in the above code, we have declared an object student,
var student = {name : "John", roll : 38, age : 12}
Now, if we want to get the name of the student, we can just type,
And the name of the student would be displayed.
Similarly, if we want to get the roll or age, the below statement works,
-
Undefined
Undefined as the name suggests, if you do no assign a value to a variable, it would have the data type as undefined.
Say, for example, the below variable x is not assigned any value,
And has the data type as undefined.
-
Function
Programmers coming from other backgrounds like Java or JavaScript might be confused that how come Function could be a data type?
Well! In JavaScript Function is also a data type.
A Function is something that performs a set of tasks. We will learn more on Functions in a separate tutorial.
For now just remember a function can be defined in the below way,