Datatype is the value, which we store in a variable in a program. These values may be of different kinds like string, number etc. In programming it is very important to know the data type perfectly. Only after this we will be able to operate on variables. Without understanding the data types a computer is not able to solve it safely.
JavaScript has eight datatypes:
1. String
2. Number
3. Bigint
4. Boolean
5. Undefined
6. Null
7. Symbol
8. Object
The Objectdatatype can contain anobject, anarray and adate.
1. String Data Type: String is the series of characters like "Peter Waterson". Strings are written with single or double quotes.
For example:
<html> <body> <h2>JavaScript Strings</h2> <p>Strings are written with single or double quotes.</p> <p id="demo"></p> <script> var Name = "Peter Waterson";var Address = 'z-10 milan road, Hawaii'; document.getElementById("demo").innerHTML = Name + "<br>" + Address; </script> </body> </html> Try it in try it editor.
Try it yourself editor
Output
2. Number Data Type: Other programming languages have many number types like:
Whole Numbers (Integers): These are byte(8 bit), short(16 bit), int(32 bit) long(64 bit).
Real Numbers (Floating Numbers): float(32 bit), double(64 bit).
JavascriptNumbers are always one type:
double(64 bit floating point). All JavaScript numbers are stored as decimal numbers (floating point). Numbers can be written with or without decimal.
For example:
<html>
<body>
<h2>JavaScript Numbers</h2>
<p>Numbers can be written with, or without decimals:</p>
<p id="demo"></p>
<script>
var x1 = 314.00;
var x2 = 314;
var x3 = 3.14;
document.getElementById("demo").innerHTML =
x1 + "<br>" + x2 + "<br>" + x3;
</script>
</body>
</html>
Try above code in try it editor:
Try it yourself editor
Output
Exponential Notation:
Extra large or extra small numbers can be written as exponential (scientific) notation.
For example:
<html>
<body>
<h2>JavaScript Numbers</h2>
<p>Extra large or extra small numbers can be written with scientific notation:</p>
3. BigInt Data Type: All JavaScript numbers are stored in a 64 bit floating number format. JavaScript bigInt is a new data type that is used to store integer values that are too big to be represented by normal JavaScript number. Mathematical calculations can't be performed between bigInt and number type.
For example:
<html>
<body>
<h1>JavScript Bigint</h1>
<p>A big integer value.</p>
<p id="demo"></p>
<p>Calculation can't be performed between bigInt and number datatype.</p>
5. Undefined Data Type: In JavaScript the variable without a value, has the value undefined and it shows 'undefined' as output. We can also give the value 'undefined' to a variable. The output will be the same.
For Example:
<html>
<body>
<h2>JavaScript undefined data type</h2>
<p>The value (and the data type) of a variable with no value is <b>undefined</b>.</p>
you can change Asia[1] or Asia[2] instead of Asia[0] and click 'Run Code' and see the output.
8. The typeof operator: JavaScript's typeof operator is used to find the type of JavaScript variable. It returns the type of variable or an expression.
<html>
<body>
<h1>JavaScript Operators</h1>
<h2>The typeof Operator</h2>
<p>The typeof operator returns the type of a variable or an expression.</p>
The proper knowledge of data type is necessary to handle the program perfectly. If we write expression like i.let x = 45 + "abc" or let x = "45" + "abc", JavaScript consider all values as string and will display 45abc. ii.Similarly, if we write like let x = "abc" + 45 or let x = "abc" + "45", the output will be as string abc45. iii. let x = 56 +90 + "abc" Here the output will be 146abc, as JavaScript evaluates expressions from left to right and in left, it will find calculation, so here it will consider 56 + 90 as number and will take"abc" as string. iv. But in let x = "abc" +56 + 90, the leftmost expression is string, so it will consider all the expression as string and output will be abc5690.