Movatterモバイル変換


[0]ホーム

URL:


Menu
×
See More 
Sign In
+1 Get Certified Upgrade Teachers Spaces Bootcamps Get Certified Upgrade Teachers Spaces Bootcamps
   ❮     
     ❯   

Basic JavaScript

JS TutorialJS SyntaxJS VariablesJS OperatorsJS If ConditionsJS LoopsJS StringsJS NumbersJS FunctionsJS ObjectsJS ScopeJS DatesJS Temporal DatesJS ArraysJS SetsJS MapsJS IterationsJS MathJS RegExpJS DestructuringJS Data TypesJS ErrorsJS DebuggingJS ConventionsJS ReferencesJS 2026JS Versions

JS HTML

JS HTML DOMJS EventsJS ProjectsNew

JS Advanced

JS FunctionsJS ObjectsJS ClassesJS AsynchronousJS ModulesJS Meta & ProxyJS Typed ArraysJS DOM NavigationJS WindowsJS Web APIsJS AJAXJS JSONJS jQueryJS GraphicsJS ExamplesJS Reference


JavaScript Data Types

AJavaScript variable can hold8 types of data.

7 Primitive Data Types and1 Object Data Type.

The Object data type can hold many different object types.

Datatypes
TypeDescription
NumberA number representing a numeric value
BigintA number representing a large integer
StringA text of characters enclosed in quotes
BooleanA data type representing true or false
UndefinedA variable with no assigned value
NullA value representing object absence
SymbolA unique primitive identifier
ObjectA collection of key-value pairs of data

Examples

// Number
let length = 16;
let weight = 7.5;

// BigInt
let x = 1234567890123456789012345n;
let y = BigInt(1234567890123456789012345)
// Strings
let color = "Yellow";
let lastName = "Johnson";

// Boolean
let x = true;
let y = false;

// Undefined
let x;
let y;

// Null
let x = null;
let y = null;

// Symbol
const x = Symbol();
const y = Symbol();

// Object
const person = {firstName:"John", lastName:"Doe"};

// Array Object
const cars = ["Saab", "Volvo", "BMW"];

// Date Object
const date = new Date("2022-03-25");

The Concept of Data Types

In programming, data types is an important concept.

To be able to operate on variables, it is important to know something about the type.

Without data types, a computer cannot safely solve this:

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?

JavaScript will treat the example above as:

let x = "16" + "Volvo";

Note

When adding a number and a string, JavaScript will treat the number as a string.

Example

let x = 16 + "Volvo";
Try it Yourself »

Example

let x = "Volvo" + 16;
Try it Yourself »

JavaScript evaluates expressions from left to right. Different sequences can produce different results:

#"tryit.asp?filename=tryjs_datatypes_addstrings_1">Try it Yourself »

#"tryit.asp?filename=tryjs_datatypes_addstrings_2">Try it Yourself »

In the first example, JavaScript treats 16 and 4 as numbers, until it reaches "Volvo".

In the second example, since the first operand is a string, all operands are treated as strings.


JavaScript Types are Dynamic

JavaScript has dynamic types. This means that the same variable can be used to hold different data types:

Example

let x;       // Now x is undefined
x = 5;       // Now x is a Number
x = "John";  // Now x is a String
Try it Yourself »


Built-In Object Types

A JavaScript object can represent aJavScript object or aUser defined object.

Built-in JavavaScript object types can be:

ObjectDescription
ArrayArray of values accessed by a numerical index
MapArray of key-value pairs where the keys can be of any data type
SetArray of values where each value can only appear once
WeakMapA type of Map with weak references to the stored objects.
WeakSetA type of Set with weak references to the stored objects.
MathAn object that provides math constants and functions like PI and random()
DateObject for working with dates and times
RegExpObject for working with regular expressions
ErrorObject represents error conditions during program execution
JSONObject with methods for parsing values between JSON and objects
PromiseObject representing the completion or failure of an asynchronous operation
Int8ArrayArray for storing fixed-size 8-bits integer values
Int16ArrayArray for storing fixed-size 16-bits integer values
Int32ArrayArray for storing fixed-size 32-bits integer values
Float16ArrayArray for storing fixed-size 16-bits floating-point values
Float32ArrayArray for storing fixed-size 32-bits floating-point values
Float64ArrayArray for storing fixed-size 64-bits floating-point values
BigInt64ArrayArray for storing fixed-size 64-bits big integer values

The typeof Operator

You can use the JavaScripttypeof operator to find the type of a JavaScript variable.

Thetypeof operator returns the type of a variable or an expression:

Example

typeof ""          // Returns "string"
typeof "John"      // Returns "string"
typeof "John Doe"  // Returns "string"
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 »


×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness
of all content. While using W3Schools, you agree to have read and accepted ourterms of use,cookies andprivacy policy.

Copyright 1999-2026 by Refsnes Data. All Rights Reserved.W3Schools is Powered by W3.CSS.

-->
[8]ページ先頭

©2009-2026 Movatter.jp