JavaScript supports a pack of built-in types. There are strings, numbers, dates, arrays, objects etc.
var foo = 42; // foo is now a Number var lastName = "Jefferson"; // String var models = ["Saab", "Volvo", "BMW"]; // Array var addr = {street:"150 feet road", postal:"12345"}; // Object
It is important to know type of the variable, before you operate on a variable. Without data types it is not possible for a computer to produce relevant output.
var x = 16 + "Volvo";
In the above example, the number 16 is automatically converted to a string “16” and then the string “Volvo” is appended to it, producing a string “16Volvo”
JavaScript is a loosely typed language. It means it is not necessary to declare type of variable. The type of variable will be automatically determined during program execution. Due to this, the same variable can have different data type, at different times.
var x; // x is undefined var x = 10; // x is Number var x = "Thing"; // x is String
A string is a continuous series of characters like “This is a cat”. String data type are always within either single quotes or double quotes.
var modalMake = "BMW 160"; // Using double quotes var modalMake = 'BMW 160'; // Using single quotes
A string can contain quotes within it provided it is not same as surrounding quotes of string.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var modalMake1 = "BMW 160";
var modalMake2 = 'BMW 160';
var answer1 = "Hi O'conol"; // Single quote inside double quotes
var answer2 = "It is known as 'famous place'"; // Single quotes inside double quotes
var answer3 = 'It is known as "famous place"'; // Double quotes inside single quotes
document.getElementById("demo").innerHTML =
modalMake1 + "<br>" +
modalMake2 + "<br>" +
answer1 + "<br>" +
answer2 + "<br>" +
answer3;
</script>
</body>
</html>
There is no separate data type in JavaScript for float, decimal, long or integer. A number can contain decimal point or can be without decimal point.
It is also possible to use scientific notation to write large numbers.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var temprature = 45.5; // Written with decimals
var int = 95; // Written without decimals
var y = 123e5;
var z = 123e-5;
document.getElementById("demo").innerHTML = temprature + "<br>" + int + "<br>" + y + "<br>" + z
</script>
</body>
</html>
Boolean data type can contain only two type of values, either true or false and mostly used with conditional operators.
var x = true; var y = false; if (x != y) { console.write ("x is not equal to y");}
An array is a collection of variables or literals. Arrays are written surrounded by square brackets. Array items are comma separated. Below code declares an array containing four elements.
<!DOCTYPE html>
<html>
<body>
<p id="demo"></p>
<script>
var carmakes = ["Audi", "BMW", "Honda", " Toyota"];
document.getElementById("demo").innerHTML = carmakes[0];
</script>
</body>
</html>
Array index always starts with [0] then [1], [2] and so on
Objects are surrounded by curly brackets. Objects contain zero or properties. Object properties are separated by commas. The property name is followed by colon (:) and then a value.
<!DOCTYPE html>
<html>
<body>
Text area properties:
<p id="demo"></p>
<script>
var textarea = {rows:"30", cols:"50", name:"note", value:"This is a test message"};
document.getElementById("demo").innerHTML =
"Rows : " + textarea.rows + "<br >" +
"Cols : " + textarea.cols + "<br >" +
"Name : " + textarea.name + "<br >" +
"Default Value: " + textarea.value + "<br >";
</script>
</body>
</html>
A variable declared without any value has default value of undefined and is of type undefined. A variable can be set to undefined any time. Note that there is no quotes around undefined text.
person = undefined;
In any programming language, null means nothing. Javascript is no exception.
var person = null; // Value is null, but type is still an object
typeof undefined // undefined typeof null // object null === undefined // false (deep compare) null == undefined // true (shallow compare)