JavaScript Data Types
JavaScript Data Types
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
1. An object
2. An array
3. A date
Examples
// Numbers:
let length = 16;
let weight = 7.5;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let xTutorials
= true; Exercises Services Sign Up Log in
let y = false;
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");
Note
A JavaScript variable can hold any type of data.
let x = 16 + "Volvo";
Does it make any sense to add "Volvo" to sixteen? Will it produce an error or will it produce a result?
Note
When adding a number and a string, JavaScript will treat the number as a string.
Tutorials Exercises Services Sign Up Log in
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
Example
let x = 16 + "Volvo";
Try it Yourself »
Example
Try it Yourself »
JavaScript evaluates expressions from left to right. Different sequences can produce different results:
JavaScript:
let x = 16 + 4 + "Volvo";
Result:
20Volvo
Try it Yourself »
JavaScript:
let x = "Volvo" + 16 + 4;
Result:
Volvo164
Try it Yourself »
In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".
Tutorials Exercises Services Sign Up Log in
In the second example, since the first operand is a string, all operands are treated as strings.
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
ADVERTISEMENT
Example
Try it Yourself »
JavaScript 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:
Example
// Using double quotes:
let carName1 = "Volvo XC60";
// Using single quotes:
Tutorials = 'Volvo
let carName2 ExercisesXC60';
Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
Try it Yourself »
You can use quotes inside a string, as long as they don't match the quotes surrounding the string:
Example
Try it Yourself »
JavaScript Numbers
All JavaScript numbers are stored as decimal numbers (floating point).
Example
// With decimals:
let x1 = 34.00;
// Without decimals:
let x2 = 34;
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
Exponential Notation
Extra large or extra small numbers can be written with scientific (exponential) notation:
Example
let y = 123e5; // 12300000
let z = 123e-5; // 0.00123
Try it Yourself »
Note
Most programming languages have many number types:
JavaScript BigInt
All JavaScript numbers are stored in a 64-bit floating-point format.
JavaScript BigInt is a new datatype (ES2020) that can be used to store integer values that are too big to be
represented by a normal JavaScript Number.
Example
let xTutorials
= BigInt("123456789012345678901234567890");
Exercises Services Sign Up Log in
HTML
Try itCSS
YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
JavaScript Booleans
Booleans can only have two values: true or false .
Example
let x = 5;
let y = 5;
let z = 6;
(x == y) // Returns true
(x == z) // Returns false
Try it Yourself »
JavaScript Arrays
JavaScript arrays are written with square brackets.
The following code declares (creates) an array called cars , containing three items (car names):
Example
constTutorials
cars = ["Saab", "Volvo", "BMW"];
Exercises Services Sign Up Log in
HTML
Try itCSS
YourselfJAVASCRIPT
» SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
Array indexes are zero-based, which means the first item is [0], second is [1], and so on.
JavaScript Objects
JavaScript objects are written with curly braces {} .
Example
Try it Yourself »
The object (person) in the example above has 4 properties: firstName, lastName, age, and eyeColor.
Example
typeof ""
Tutorials
typeof "John"
// Returns "string"
Exercises Services
// Returns "string"
Sign Up Log in
Try it Yourself »
Example
typeof 0 // Returns "number"
typeof 314 // Returns "number"
typeof 3.14 // Returns "number"
typeof (3) // Returns "number"
typeof (3 + 4) // Returns "number"
Try it Yourself »
Undefined
In JavaScript, a variable without a value, has the value undefined . The type is also undefined .
Example
let car; // Value is undefined, type is undefined
Try it Yourself »
Any variable can be emptied, by setting the value to undefined . The type will also be undefined .
Example
car = undefined; // Value is undefined, type is undefined
Try it Yourself »
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
Empty Values
An empty value has nothing to do with undefined .
Example
let car = ""; // The value is "", the typeof is "string"
Try it Yourself »
Exercise:
Use comments to describe the correct data type of the following variables:
Submit Answer »
Start the Exercise
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
❮ Previous Next ❯
W3schools Pathfinder
Track your progress - it's free! Sign Up Log in
ADVERTISEMENT
COLOR PICKER
ADVERTISEMENT
Tutorials Exercises Services Sign Up Log in
HTML
CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++
FORUM ABOUT
Tutorials Exercises Services Sign Up
W3Schools is optimized for learning and training. Examples might be simplified to improve reading and
Log in
learning.
HTML
CSS JAVASCRIPT
Tutorials, SQL
references, and examples PYTHON
are JAVAto avoidPHP
constantly reviewed errors, butHOW TO warrant
we cannot W3.CSS
full C C++
correctness
of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and
privacy policy.
Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.