Movatterモバイル変換


[0]ホーム

URL:


MDN Web Docs

Grammar and types

This chapter discusses JavaScript's basic grammar, variable declarations, data types and literals.

Basics

JavaScript borrows most of its syntax from Java, C, and C++, but it has also been influenced by Awk, Perl, and Python.

JavaScript iscase-sensitive and uses theUnicode character set. For example, the word Früh (which means "early" in German) could be used as a variable name.

js
const Früh = "foobar";

But, the variablefrüh is not the same asFrüh because JavaScript is case sensitive.

In JavaScript, instructions are calledstatements and are separated by semicolons (;).

A semicolon is not necessary after a statement if it is written on its own line. But if more than one statement on a line is desired, then theymust be separated by semicolons.

Note:ECMAScript also has rules for automatic insertion of semicolons (ASI) to end statements. (For more information, see the detailed reference about JavaScript'slexical grammar.)

It is considered best practice, however, to always write a semicolon after a statement, even when it is not strictly needed. This practice reduces the chances of bugs getting into the code.

The source text of JavaScript script gets scanned from left to right, and is converted into a sequence of input elements which aretokens,control characters,line terminators,comments, orwhitespace. (Spaces, tabs, and newline characters are considered whitespace.)

Comments

The syntax ofcomments is the same as in C++ and in many other languages:

js
// a one line comment/* this is a longer, * multi-line comment */

You can't nest block comments. This often happens when you accidentally include a*/ sequence in your comment, which will terminate the comment.

js
/* You can't, however, /* nest comments */ SyntaxError */

In this case, you need to break up the*/ pattern. For example, by inserting a backslash:

js
/* You can /* nest comments *\/ by escaping slashes */

Comments behave like whitespace, and are discarded during script execution.

Note:You might also see a third type of comment syntax at the start of some JavaScript files, which looks something like this:#!/usr/bin/env node.

This is calledhashbang comment syntax, and is a special comment used to specify the path to a particular JavaScript engine that should execute the script. SeeHashbang comments for more details.

Declarations

JavaScript has three kinds of variable declarations.

var

Declares a variable, optionally initializing it to a value.

let

Declares a block-scoped, local variable, optionally initializing it to a value.

const

Declares a block-scoped, read-only named constant.

Variables

You use variables as symbolic names for values in your application. The names of variables, calledidentifiers, conform to certain rules.

A JavaScript identifier usually starts with a letter, underscore (_), or dollar sign ($). Subsequent characters can also be digits (09). Because JavaScript is case sensitive, letters include the charactersA throughZ (uppercase) as well asa throughz (lowercase).

You can use most Unicode letters such aså andü in identifiers. (For more details, see thelexical grammar reference.) You can also useUnicode escape sequences to represent characters in identifiers.

Some examples of legal names areNumber_hits,temp99,$credit, and_name.

Declaring variables

You can declare a variable in two ways:

  • With the keywordvar. For example,var x = 42. This syntax can be used to declare bothlocal andglobal variables, depending on theexecution context.
  • With the keywordconst orlet. For example,let y = 13. This syntax can be used to declare a block-scope local variable. (SeeVariable scope below.)

You can declare variables to unpack values using thedestructuring syntax. For example,const { bar } = foo. This will create a variable namedbar and assign to it the value corresponding to the key of the same name from our objectfoo.

Variables should always be declared before they are used. JavaScript used to allow assigning to undeclared variables, which creates anundeclared global variable. This is an error instrict mode and should be avoided altogether.

Declaration and initialization

In a statement likelet x = 42, thelet x part is called adeclaration, and the= 42 part is called aninitializer. The declaration allows the variable to be accessed later in code without throwing aReferenceError, while the initializer assigns a value to the variable. Invar andlet declarations, the initializer is optional. If a variable is declared without an initializer, it is assigned the valueundefined.

js
let x;console.log(x); // logs "undefined"

In essence,let x = 42 is equivalent tolet x; x = 42.

const declarations always need an initializer, because they forbid any kind of assignment after declaration, and implicitly initializing it withundefined is likely a programmer mistake.

js
const x; // SyntaxError: Missing initializer in const declaration

Variable scope

A variable may belong to one of the followingscopes:

  • Global scope: The default scope for all code running in script mode.
  • Module scope: The scope for code running in module mode.
  • Function scope: The scope created with afunction.

In addition, variables declared withlet orconst can belong to an additional scope:

  • Block scope: The scope created with a pair of curly braces (ablock).

When you declare a variable outside of any function, it is called aglobal variable, because it is available to any other code in the current document. When you declare a variable within a function, it is called alocal variable, because it is available only within that function.

let andconst declarations can also be scoped to theblock statement that they are declared in.

js
if (Math.random() > 0.5) {  const y = 5;}console.log(y); // ReferenceError: y is not defined

However, variables created withvar are not block-scoped, but only local to thefunction (or global scope) that the block resides within.

For example, the following code will log5, because the scope ofx is the global context (or the function context if the code is part of a function). The scope ofx is not limited to the immediateif statement block.

js
if (true) {  var x = 5;}console.log(x); // x is 5

Variable hoisting

var-declared variables arehoisted, meaning you can refer to the variable anywhere in its scope, even if its declaration isn't reached yet. You can seevar declarations as being "lifted" to the top of its function or global scope. However, if you access a variable before it's declared, the value is alwaysundefined, because only itsdeclaration anddefault initialization (withundefined) is hoisted, but not itsvalue assignment.

js
console.log(x === undefined); // truevar x = 3;(function () {  console.log(x); // undefined  var x = "local value";})();

The above examples will be interpreted the same as:

js
var x;console.log(x === undefined); // truex = 3;(function () {  var x;  console.log(x); // undefined  x = "local value";})();

Because of hoisting, allvar statements in a function should be placed as near to the top of the function as possible. This best practice increases the clarity of the code.

Whetherlet andconst are hoisted is a matter of definition debate. Referencing the variable in the block before the variable declaration always results in aReferenceError, because the variable is in a "temporal dead zone" from the start of the block until the declaration is processed.

js
console.log(x); // ReferenceErrorconst x = 3;console.log(y); // ReferenceErrorlet y = 3;

Unlikevar declarations, which only hoist the declaration but not its value,function declarations are hoisted entirely — you can safely call the function anywhere in its scope. See thehoisting glossary entry for more discussion.

Global variables

Global variables are in fact properties of theglobal object.

In web pages, the global object iswindow, so you can read and set global variables using thewindow.variable syntax. In all environments, theglobalThis variable (which itself is a global variable) may be used to read and set global variables. This is to provide a consistent interface among various JavaScript runtimes.

Consequently, you can access global variables declared in one window or frame from another window or frame by specifying thewindow orframe name. For example, if a variable calledphoneNumber is declared in a document, you can refer to this variable from aniframe asparent.phoneNumber.

Constants

You can create a read-only, named constant with theconst keyword. The syntax of a constant identifier is the same as any variable identifier: it must start with a letter, underscore, or dollar sign ($), and can contain alphabetic, numeric, or underscore characters.

js
const PI = 3.14;

A constant cannot change value through assignment or be re-declared while the script is running. It must be initialized to a value. The scope rules for constants are the same as those forlet block-scope variables.

You cannot declare a constant with the same name as a function or variable in the same scope. For example:

js
// THIS WILL CAUSE AN ERRORfunction f() {}const f = 5;// THIS WILL CAUSE AN ERROR TOOfunction f() {  const g = 5;  var g;}

However,const only preventsre-assignments, but doesn't preventmutations. The properties of objects assigned to constants are not protected, so the following statement is executed without problems.

js
const MY_OBJECT = { key: "value" };MY_OBJECT.key = "otherValue";

Also, the contents of an array are not protected, so the following statement is executed without problems.

js
const MY_ARRAY = ["HTML", "CSS"];MY_ARRAY.push("JAVASCRIPT");console.log(MY_ARRAY); // ['HTML', 'CSS', 'JAVASCRIPT'];

Data structures and types

Data types

The latest ECMAScript standard defines eight data types:

  • Seven data types that areprimitives:

    1. Boolean.true andfalse.
    2. null. A special keyword denoting a null value. (Because JavaScript is case-sensitive,null is not the same asNull,NULL, or any other variant.)
    3. undefined. A top-level property whose value is not defined.
    4. Number. An integer or floating point number. For example:42 or3.14159.
    5. BigInt. An integer with arbitrary precision. For example:9007199254740992n.
    6. String. A sequence of characters that represent a text value. For example:"Howdy".
    7. Symbol. A data type whose instances are unique and immutable.
  • andObject

Although these data types are relatively few, they enable you to perform useful operations with your applications.Functions are the other fundamental elements of the language. While functions are technically a kind of object, you can think of objects as named containers for values, and functions as procedures that your script can perform.

Data type conversion

JavaScript is adynamically typed language. This means you don't have to specify the data type of a variable when you declare it. It also means that data types are automatically converted as-needed during script execution.

So, for example, you could define a variable as follows:

js
let answer = 42;

And later, you could assign the same variable a string value, for example:

js
answer = "Thanks for all the fish!";

Because JavaScript is dynamically typed, this assignment does not cause an error message.

Numbers and the '+' operator

In expressions involving numeric and string values with the+ operator, JavaScript converts numeric values to strings. For example, consider the following statements:

js
x = "The answer is " + 42; // "The answer is 42"y = 42 + " is the answer"; // "42 is the answer"z = "37" + 7; // "377"

With all other operators, JavaScript doesnot convert numeric values to strings. For example:

js
"37" - 7; // 30"37" * 7; // 259

Converting strings to numbers

In the case that a value representing a number is in memory as a string, there are methods for conversion.

parseInt only returns whole numbers, so its use is diminished for decimals.

Note:Additionally, a best practice forparseInt is to always include theradix parameter. The radix parameter is used to specify which numerical system is to be used.

js
parseInt("101", 2); // 5

An alternative method of retrieving a number from a string is with the+ (unary plus) operator. This implicitly performsnumber conversion, which is the same process as theNumber() function.

js
"1.1" + "1.1"; // '1.11.1'(+"1.1") + (+"1.1"); // 2.2// Note: the parentheses are added for clarity, not required.

Literals

Literals represent values in JavaScript. These are fixed values—not variables—that youliterally provide in your script. This section describes the following types of literals:

Array literals

An array literal is a list of zero or more expressions, each of which represents an array element, enclosed in square brackets ([]). When you create an array using an array literal, it is initialized with the specified values as its elements, and itslength is set to the number of arguments specified.

The following example creates thecoffees array with three elements and alength of three:

js
const coffees = ["French Roast", "Colombian", "Kona"];

An array literal creates a new array object every time the literal is evaluated. For example, an array defined with a literal in the global scope is created once when the script loads. However, if the array literal is inside a function, a new array is instantiated every time that function is called.

Note:Array literals createArray objects. SeeArray andIndexed collections for details onArray objects.

Extra commas in array literals

If you put two commas in a row in an array literal, the array leaves an empty slot for the unspecified element. The following example creates thefish array:

js
const fish = ["Lion", , "Angel"];

When you log this array, you will see:

js
console.log(fish);// [ 'Lion', <1 empty item>, 'Angel' ]

Note that the second item is "empty", which is not exactly the same as the actualundefined value. When using array-traversing methods likeArray.prototype.map, empty slots are skipped. However, index-accessingfish[1] still returnsundefined.

If you include a trailing comma at the end of the list of elements, the comma is ignored.

In the following example, thelength of the array is three. There is nomyList[3] andmyList[1] is empty. All other commas in the list indicate a new element.

js
const myList = ["home", , "school"];

In the following example, thelength of the array is four, andmyList[0] andmyList[2] are missing.

js
const myList = [, "home", , "school"];

In the following example, thelength of the array is four, andmyList[1] andmyList[3] are missing.Only the last comma is ignored.

js
const myList = ["home", , "school", ,];

Note:Trailing commas help keep git diffs clean when you have a multi-line array, because appending an item to the end only adds one line, but does not modify the previous line.

diff
const myList = [  "home",  "school",+ "hospital",];

Understanding the behavior of extra commas is important to understanding JavaScript as a language.

However, when writing your own code, you should explicitly declare the missing elements asundefined, or at least insert a comment to highlight its absence. Doing this increases your code's clarity and maintainability.

js
const myList = ["home", /* empty */, "school", /* empty */, ];

Boolean literals

The Boolean type has two literal values:true andfalse.

Note:Do not confuse the primitive Boolean valuestrue andfalse with the true and false values of theBoolean object.

The Boolean object is a wrapper around the primitive Boolean data type. SeeBoolean for more information.

Numeric literals

JavaScript numeric literals include integer literals in different bases as well as floating-point literals in base-10.

Note that the language specification requires numeric literals to be unsigned. Nevertheless, code fragments like-123.4 are fine, being interpreted as a unary- operator applied to the numeric literal123.4.

Integer literals

Integer andBigInt literals can be written in decimal (base 10), hexadecimal (base 16), octal (base 8) and binary (base 2).

  • Adecimal integer literal is a sequence of digits without a leading0 (zero).
  • A leading0 (zero) on an integer literal, or a leading0o (or0O) indicates it is inoctal. Octal integer literals can include only the digits07.
  • A leading0x (or0X) indicates ahexadecimal integer literal. Hexadecimal integers can include digits (09) and the lettersaf andAF. (The case of a character does not change its value. Therefore:0xa =0xA =10 and0xf =0xF =15.)
  • A leading0b (or0B) indicates abinary integer literal. Binary integer literals can only include the digits0 and1.
  • A trailingn suffix on an integer literal indicates aBigInt literal. TheBigInt literal can use any of the above bases. Note that leading-zero octal syntax like0123n is not allowed, but0o123n is fine.

Some examples of integer literals are:

0, 117, 123456789123456789n             (decimal, base 10)015, 0001, 0o777777777777n              (octal, base 8)0x1123, 0x00111, 0x123456789ABCDEFn     (hexadecimal, "hex" or base 16)0b11, 0b0011, 0b11101001010101010101n   (binary, base 2)

For more information, seeNumeric literals in the Lexical grammar reference.

Floating-point literals

A floating-point literal can have the following parts:

  • An unsigned decimal integer,
  • A decimal point (.),
  • A fraction (another decimal number),
  • An exponent.

The exponent part is ane orE followed by an integer, which can be signed (preceded by+ or-). A floating-point literal must have at least one digit, and either a decimal point ore (orE).

More succinctly, the syntax is:

[digits].[digits][(E|e)[(+|-)]digits]

For example:

js
3.1415926.1234567893.1E+12.1e-23

Object literals

An object literal is a list of zero or more pairs of property names and associated values of an object, enclosed in curly braces ({}).

Warning:Do not use an object literal at the beginning of a statement! This will lead to an error (or not behave as you expect), because the{ will be interpreted as the beginning of a block.

The following is an example of an object literal. The first element of thecar object defines a property,myCar, and assigns to it a new string,"Saturn"; the second element, thegetCar property, is immediately assigned the result of invoking the function(carTypes("Honda")); the third element, thespecial property, uses an existing variable (sales).

js
const sales = "Toyota";function carTypes(name) {  return name === "Honda" ? name : `Sorry, we don't sell ${name}.`;}const car = { myCar: "Saturn", getCar: carTypes("Honda"), special: sales };console.log(car.myCar); // Saturnconsole.log(car.getCar); // Hondaconsole.log(car.special); // Toyota

Additionally, you can use a numeric or string literal for the name of a property or nest an object inside another. The following example uses these options.

js
const car = { manyCars: { a: "Saab", b: "Jeep" }, 7: "Mazda" };console.log(car.manyCars.b); // Jeepconsole.log(car[7]); // Mazda

Object property names can be any string, including the empty string. If the property name would not be a valid JavaScriptidentifier or number, it must be enclosed in quotes.

Property names that are not valid identifiers cannot be accessed as a dot (.) property.

js
const unusualPropertyNames = {  "": "An empty string",  "!": "Bang!",};console.log(unusualPropertyNames.""); // SyntaxError: Unexpected stringconsole.log(unusualPropertyNames.!); // SyntaxError: Unexpected token !

Instead, they must be accessed with the bracket notation ([]).

js
console.log(unusualPropertyNames[""]); // An empty stringconsole.log(unusualPropertyNames["!"]); // Bang!

Enhanced Object literals

Object literals support a range of shorthand syntaxes that include setting the prototype at construction, shorthand forfoo: foo assignments, defining methods, makingsuper calls, and computing property names with expressions.

Together, these also bring object literals and class declarations closer together, and allow object-based design to benefit from some of the same conveniences.

js
const obj = {  // __proto__  __proto__: theProtoObj,  // Shorthand for 'handler: handler'  handler,  // Methods  toString() {    // Super calls    return `d ${super.toString()}`;  },  // Computed (dynamic) property names  ["prop_" + (() => 42)()]: 42,};

RegExp literals

A regex literal (which is defined in detaillater) is a pattern enclosed between slashes. The following is an example of a regex literal.

js
const re = /ab+c/;

String literals

A string literal is zero or more characters enclosed in double (") or single (') quotation marks. A string must be delimited by quotation marks of the same type (that is, either both single quotation marks, or both double quotation marks).

The following are examples of string literals:

js
'foo'"bar"'1234''one line \n another line'"Joyo's cat"

You should use string literals unless you specifically need to use aString object. SeeString for details onString objects.

You can call any of theString object's methods on a string literal value. JavaScript automatically converts the string literal to a temporary String object, calls the method, then discards the temporary String object. You can also use thelength property with a string literal:

js
// Will print the number of symbols in the string including whitespace.console.log("Joyo's cat".length); // In this case, 10.

Template literals are also available. Template literals are enclosed by the back-tick (`) (grave accent) character instead of double or single quotes.

Template literals provide syntactic sugar for constructing strings. (This is similar to string interpolation features in Perl, Python, and more.)

js
// Basic literal string creation`In JavaScript '\n' is a line-feed.`;// Multiline strings`In JavaScript, template strings can run over multiple lines, but double and single quoted strings cannot.`;// String interpolationconst name = "Lev",  time = "today";`Hello ${name}, how are you ${time}?`;

Tagged templates are a compact syntax for specifying a template literal along with a call to a "tag" function for parsing it. A tagged template is just a more succinct and semantic way to invoke a function that processes a string and a set of relevant values. The name of the template tag function precedes the template literal — as in the following example, where the template tag function is namedprint. Theprint function will interpolate the arguments and serialize any objects or arrays that may come up, avoiding the pesky[object Object].

js
const formatArg = (arg) => {  if (Array.isArray(arg)) {    // Print a bulleted list    return arg.map((part) => `- ${part}`).join("\n");  }  if (arg.toString === Object.prototype.toString) {    // This object will be serialized to "[object Object]".    // Let's print something nicer.    return JSON.stringify(arg);  }  return arg;};const print = (segments, ...args) => {  // For any well-formed template literal, there will always be N args and  // (N+1) string segments.  let message = segments[0];  segments.slice(1).forEach((segment, index) => {    message += formatArg(args[index]) + segment;  });  console.log(message);};const todos = [  "Learn JavaScript",  "Learn Web APIs",  "Set up my website",  "Profit!",];const progress = { #"javascript":20,"html":50,"css":10}

Since tagged template literals are just sugar of function calls, you can re-write the above as an equivalent function call:

js
print(["I need to do:\n", "\nMy current progress is: ", "\n"], todos, progress);

This may be reminiscent of theconsole.log-style interpolation:

js
console.log("I need to do:\n%o\nMy current progress is: %o\n", todos, progress);

You can see how the tagged template reads more naturally than a traditional "formatter" function, where the variables and the template itself have to be declared separately.

Using special characters in strings

In addition to ordinary characters, you can also include special characters in strings, as shown in the following example.

js
"one line \n another line";

The following table lists the special characters that you can use in JavaScript strings.

CharacterMeaning
\0Null Byte
\bBackspace
\fForm Feed
\nNew Line
\rCarriage Return
\tTab
\vVertical tab
\'Apostrophe or single quote
\"Double quote
\\Backslash character
\XXXThe character with the Latin-1 encoding specified by up to three octal digitsXXX between0 and377. For example,\251 is the octal sequence for the copyright symbol.
\xXXThe character with the Latin-1 encoding specified by the two hexadecimal digitsXX between00 andFF. For example,\xA9 is the hexadecimal sequence for the copyright symbol.
\uXXXXThe Unicode character specified by the four hexadecimal digitsXXXX. For example,\u00A9 is the Unicode sequence for the copyright symbol. SeeUnicode escape sequences.
\u{XXXXX}Unicode code point escapes. For example,\u{2F804} is the same as the Unicode escapes\uD87E\uDC04.

Escaping characters

For characters not listed in the table, a preceding backslash is ignored, but this usage is deprecated and should be avoided.

You can insert a quotation mark inside a string by preceding it with a backslash. This is known asescaping the quotation mark. For example:

js
const quote = "He read \"The Cremation of Sam McGee\" by R.W. Service.";console.log(quote);

The result of this would be:

He read "The Cremation of Sam McGee" by R.W. Service.

To include a literal backslash inside a string, you must escape the backslash character. For example, to assign the file pathc:\temp to a string, use the following:

js
const home = "c:\\temp";

You can also escape line breaks by preceding them with backslash. The backslash and line break are both removed from the value of the string.

js
const str =  "this string \is broken \across multiple \lines.";console.log(str); // this string is broken across multiple lines.

More information

This chapter focuses on basic syntax for declarations and types. To learn more about JavaScript's language constructs, see also the following chapters in this guide:

In the next chapter, we will have a look at control flow constructs and error handling.

Help improve MDN

Learn how to contribute.

This page was last modified on byMDN contributors.


[8]ページ先頭

©2009-2025 Movatter.jp