- Home
- javascript Tutorial
- datatypes
Datatypes
JavaScript provides different data types to hold different types of values. There are two types of data types in JavaScript.
- Primitive data type
- Non-primitive (reference) data type
JavaScript is a dynamic type language, means the type of the variable does not need to be explicitly specified.This is because the type of the variable is dynamically assigned by the javascript engine. A variable can be declared using the keyword var. var can hold any type of values such as numbers, strings etc.
There are five types of primitive data types in JavaScript.
- String- represents a sequence of characters e.g. "hello"
- Number- represents numeric values e.g. 100
- Boolean- represents boolean value either false or true
- Undefined- represents an undefined value
- Null- represents null i.e. no value at all
- The non-primitive data types are:
- Object- represents instance through which we can access members
- Array- represents a group of similar values
- RegExp- represents regular expression
Primitive data types:
String:
A string (or a text string) is a series of characters. Strings are written with quotes. They can be represented by using either of single or double quotes.
Example:
var carName = "Volvo"; // Using double quotes
var carName = 'Volvo'; // Using single quotes
Example:
<html>
<body>
<p id="demo"></p>
<script>
var carName = "Volvo";
var answer = 'Its alright';
document.getElementById("demo").innerHTML =
carName + "<br>" +
answer;
</script>
</body>
</html>
Output:
Its alright
Number:
JavaScript has only one type of numbers. Numbers can be written with, or without decimals.
Example:
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
Note: Extra large or extra small numbers can be written with scientific (exponential) notation.
Example:
<html>
<body>
<p id="demo"></p>
<script>
var x1 = 34.00;
var x2 = 34;
var y = 123e5;
var z = 123e-5;
document.getElementById("demo").innerHTML = x1 + "<br>" + x2 + "<br>" + y + "<br>" + z
</script>
</body>
</html>
Output:
34
12300000
0.00123
Boolean:
Booleans can only have two values: true or false.
Example:
var x = true;
var y = false;
Booleans are often used in conditional testing.
Non-primitive data types:
Arrays
JavaScript arrays are written with square brackets. The items in the Array are separated by commas.
Example:
<html>
<body>
<p id="demo"></p>
<script>
var cars = ["Saab","Volvo","BMW"];
document.getElementById("demo").innerHTML = cars[0];
</script>
</body>
</html>
Output:
Objects
JavaScript objects are written with curly braces.
Object properties are written in key:value pairs, which are separated by commas.
Example:
<html>
<body>
<p id="demo"></p>
<script>
var person = {
firstName : "John",
lastName : "Doe",
age : 50,
eyeColor : "blue"
};
document.getElementById("demo").innerHTML =
person.firstName + " is " + person.age + " years old.";
</script>
</body>
</html>
Output:
The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.