JavaScript Data Types
JavaScript Data Types
JavaScript: numbers, strings, booleans, objects, and arrays.
languages use classification for a better coding experience and bug-free code.
For the program to know how to treat a variable, it must know its data type.
Example:
var year = 2016; // Number
var firstName = "Wilson"; // String
var person = {name:"Wilson", age:"old"}; // Object
var person = ["Wilson", "old"]; //array
var x=null; // null
var x; //undefined
Strings
Strings are JavaScript data types that hold information in text. They are encapsulated with
quotes (unlike numbers, which do not need them). Both double and single quotes are
accepted.
var text = "It's sunny outside"; // Single quote inside double quotes
var text = "It is called 'Audi'"; // Single quotes inside double quotes
var text = 'It is called "Audi"'; // Double quotes inside single quotes
Numbers
JavaScript numbers can be written with or without decimals. As we already covered, they
Objects
One of JavaScript types of data are objects which are defined with curly braces. The object
type must have a property and a value: they are written in propertyName:value pairs.
var human = {
firstName: "Bob",
lastName: "White",
age: 44,
eyeColor: "brown"
};
Booleans
A boolean is a data type that can have only on of two possible values: true or false.
Arrays
JavaScript arrays are defined with square brackets. Arrays are one of those JavaScript data
types that are commonly used with variables. Array items are normally separated with
commas.
If a variable does not have a value, JavaScript automatically sets it as undefined. In the
example below, the variable has a name but does not have a value assigned. Without the
value, we do not know what is the type of this variable, which makes it undefined. If you use
return undefined only:
Example
is an object. You can clear an object by setting it to JavaScript null. This also means that
Example
NOTE:
There are a few JavaScript types of data, which serve a different purpose in the
code.
your code.
You can use JavaScript typeOf operator to check what type of JavaScript data
something is.