Strings: A string (or a text string) is a series of characters like "John Doe". Strings are written with quotes. You can use single or double quotes:
var carName1 = "Volvo XC60"; // Using double quotes
var carName2 = 'Volvo XC60'; // Using single quotes
Numbers: JavaScript has only one type of numbers. Numbers can be written with, or without decimals.
var x1 = 34.00; // Written with decimals
var x2 = 34; // Written without decimals
Booleans: Booleans can only have two values: true or false.
var x = 5;
var y = 5;
var z = 6;
(x == y) // Returns true
(x == z) // Returns false
Arrays: JavaScript arrays are written with square brackets. Array items are separated by commas. The following code declares (creates) an array called cars, containing three items (car names):
var cars = ["Saab", "Volvo", "BMW"];
Objects: JavaScript objects are written with curly braces {}. Object properties are written as name:value pairs, separated by commas.
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
The typeof Operator:You can use the JavaScript typeof operator to find the type of a JavaScript variable. The typeof operator returns the type of a variable or an expression.
typeof "" // Returns "string"
typeof "John" // Returns "string"
typeof "John Doe" // Returns "string"
typeof 0 // Returns "number"
typeof 314 // Returns "number"
typeof 3.14 // Returns "number"
typeof (3) // Returns "number"
typeof (3 + 4) // Returns "number"
Undefined: A variable without a value, has the value undefined. The type is also undefined. Any variable can be emptied, by setting the value to undefined. The type will also be undefined.
var car; // Value is undefined, type is undefined
car = undefined; // Value is undefined, type is undefined
Empty Values: An empty value has nothing to do with undefined. An empty string has both a legal value and a type.
var car; // Value is undefined, type is undefined
Null: In JavaScript null is "nothing". It is supposed to be something that doesn't exist. Unfortunately, in JavaScript, the data type of null is an object. You can consider it a bug in JavaScript that typeof null is an object. It should be null. You can empty an object by setting it to null:You can also empty an object by setting it to undefined.
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null; // Now value is null, but type is still an object
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined; // Now both value and type is undefined
Difference Between Undefined and Null undefined and null are equal in value but different in type:
typeof undefined // undefined
typeof null // object
null === undefined // false
null == undefined // true
J avaScript variables can hold many data types: numbers, strings, booleabs, arrays, objects and more: To be able to operate on variables, it is important to know something about the type.