Dynamic Data Types
JavaScript has dynamic data types. The same JavaScript variable can be treated as having different data types depending on the context it is used in.
var travelType; // travelType is undefined
var travelType = 5; // travelType is a Number
var travelType = "Car"; // travelType is a String
Internal JavaScript data types:
See also Objects.
String
A string in JavaScript is series of characters enclosed in quotation marks. A string must be delimited by quotation marks of the same type, either single quotation marks ' or double quotation marks ".
var name = "John Smith";
var type = 'customer';
You can use quotes inside a string, as long as they don't match the quotes surrounding the string.
var responseText = "It was paid to 'John Smith'";
var responseText = 'It was paid to "John Smith"';
You can put a quote inside a string using the \ character.
var responseText = 'It\'s okay.';
You can access a character in a string by its zero-based position index.
var name = "John Smith";
var character = name[3]; // character == 'n'
In JavaScript a string is an object. See String for properties and methods.
Number
JavaScript has only one type of number. Large numbers can be written in scientific (exponential) notation.
var pi = 3.14;
var amount = 314e5; // 31400000
var factor = 314e-5; // 0.00314
JavaScript interprets numeric constants as octal if they are preceded by a zero, and as hexadecimal if they are preceded by a zero and x.
var x=0377; // This is 255 in decimal
var y=0xFF; // This is 255 in decimal
When you assign a number to a variable, do not put quotes around the value. If you put quotes around a numeric value, the variable content will be treated as a string.
Never write a number with a leading zero, unless you want an octal conversion.
In JavaScript a number is an object. See Number for properties and methods.
Boolean
Booleans can only have two values: true or false.
var sent = true;
var paid = false;
In JavaScript a Boolean is an object. See Boolean for properties and methods.
null
null is a special keyword denoting an empty value.
A variable can be emptied by setting it to null.
travelType = null;
undefined
This is a special keyword denoting an undefined value.
Before a variable is assigned a value it is undefined.
var travelType; // variable is undefined