This site is mobile accessible. Press the "Tap Here" button to use a different font-size.
Smartphone icons created by Freepik - Flaticon
JavaScript variables can hold many data types: numbers, strings, objects and more:
var length = 16; // Number var lastName = "Johnson"; // String var x = {firstName:"John", lastName:"Doe"}; // Object
var x = 16 + "Volvo";
var x = "16" + "Volvo";
When adding a number and a string, JavaScript will treat the number as a string.
var x = 16 + "Volvo";
var x = "Volvo" + 16;
JavaScript evaluates expressions from left to right. Different sequences can produce different results:
var x = 16 + 4 + "Volvo";
var x = "Volvo" + 16 + 4;
JavaScript has dynamic types. This means that the same variable can be used to hold different data types:
var x; // Now x is undefined x = 5; // Now x is a Number x = "John"; // Now x is a String
var carName1 = "Volvo XC60"; // Using double quotes var carName2 = 'Volvo XC60'; // Using single quotes
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
var answer1 = "It's alright"; // Single quote inside double quotes var answer2 = "He is called 'Johnny'"; // Single quotes inside double quotes var answer3 = 'He is called "Johnny"'; // Double quotes inside single quotes
You will learn more about strings later in this tutorial.
var x1 = 34.00; // Written with decimals var x2 = 34; // Written without decimals
Extra large or extra small numbers can be written with scientific (exponential) notation:
var y = 123e5; // 12300000 var z = 123e-5; // 0.00123
You will learn more about numbers later in this tutorial.
Booleans can only have two values:
or .var x = 5; var y = 5; var z = 6; (x == y) // Returns true (x == z) // Returns false
var cars = ["Saab", "Volvo", "BMW"];
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
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"
In JavaScript, a variable without a value, has the value
. The type is also .var car; // Value is undefined, type is undefined
Any variable can be emptied, by setting the value to undefined. The type will also be undefined.
car = undefined; // Value is undefined, type is undefined
var car = ""; // The value is "", the typeof is "string"
You can consider it a bug in JavaScript that
is an object. It should be .You can empty an object by setting it to
:var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; person = null; // Now value is null, but type is still an object
You can also empty an object by setting it to undefined:
var person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"}; person = undefined; // Now both value and type is undefined
and are equal in value but different in type:
typeof undefined // undefined typeof null // object null === undefined // false null == undefined // true
typeof "John" // Returns "string" typeof 3.14 // Returns "number" typeof true // Returns "boolean" typeof false // Returns "boolean" typeof x // Returns "undefined" (if x has no value)
typeof {name:'John', age:34} // Returns "object" typeof [1,2,3,4] // Returns "object" (not "array", see note below) typeof null // Returns "object" typeof function myFunc(){} // Returns "function"
The
operator returns " " for arrays because in JavaScript arrays are objects.Eventually the navigation links, above, will be replaced by these (previous) and (next) buttons below.
JavaScript icons used in the buttons provided by ICONS8.COM. Smartphone icons created by Freepik - Flaticon