[ Web Proxy ]
URL:
Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Grammar_and_types [Back]  [Original]

- JavaScript | MDN

MDN Web Docs

View in English Always switch to English

JavaScript

JavaScript Java, C, C++ Awk, Perl, Python

JavaScript Unicode Frh ( "early" )

js
const Frh = "foobar";

JavaScript frh Frh

JavaScript (statement) (;)

: ECMAScript (ASI)JavaScript

JavaScript

C++

js
// 

/* 
   
 */

*/

js
/* /*  */ SyntaxError  */

*/

js
/*  /*  *\/  */

: JavaScript #!/usr/bin/env node

JavaScript

JavaScript 3

var

let

const

JavaScript (_) ($) (09) JavaScript "A" "Z" "a" "z"

Unicode Unicode

Number_hitstemp99_name

2

const { bar } = foo bar foo

JavaScript

let x = 42 let x = 42 ReferenceError var let undefined

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

let x = 42 let x; x = 42

const undefined

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

  • :
  • :
  • :

let const

  • :

let const

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

var

5 x x if

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

var var undefined undefined

js
console.log(x === undefined); // true
var x = 3;

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

js
var x;
console.log(x === undefined); // true
x = 3;

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

var

let const ReferenceError

js
console.log(x); // ReferenceError
const x = 3;

console.log(y); // ReferenceError
let y = 3;

var

window window. globalThis JavaScript

window frame phoneNumber iframe parent.phoneNumber

const ($)

js
const PI = 3.14;

let

js
// THIS WILL CAUSE AN ERROR
function f() {}
const f = 5;

// 
function f() {
  const g = 5;
  var g;
}

const

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

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

ECMAScript 8

  • 7

    1. true false
    2. nullnull JavaScript null Null NULL
    3. undefined
    4. 42 3.14159
    5. 9007199254740992n
    6. "Howdy"

JavaScript

js
let answer = 42;

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

JavaScript

'+'

+ JavaScript

js
x = " " + 42; // " 42"
y = 42 + " "; // "42 "
z = "37" + 7; // "377"

JavaScript

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

parseInt

: parseInt

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

+ () Number()

js
"1.1" + "1.1"; // '1.11.1'
(+"1.1") + (+"1.1"); // 2.2
// : 

JavaScript

0 ([]) (length)

3 coffees (length) 3

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

: Array Array Array

2 fish

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

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

2 "empty" undefined Array.prototype.map fish[1] undefined

(length) 3 myList[3] myList[1]

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

(length) 4 myList[0] myList[2]

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

(length) 4 myList[1] myList[3]

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

:> git diff

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

JavaScript

undefined

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

2 true false

: true false Boolean true false

Boolean Boolean

JavaScript 10

-123.4 - 123.4

(BigInt) 10 16 8 2

  • 10 0
  • 0 0o 0O8 8 0 7
  • 0x 0X16 16 (0 9) a f A F 0xa = 0xA = 10 0xf = 0xF = 15
  • 0b 0B2 2 0 1
  • n (BigInt) 0123n 0 8 0o123n

0, 117, 123456789123456789n             10 
015, 0001, 0o777777777777n              8 
0x1123, 0x00111, 0x123456789ABCDEFn     16 
0b11, 0b0011, 0b11101001010101010101n   2 

  • 10
  • (.)
  • 10

e E + - 1 e E

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

js
3.1415926
.123456789
3.1E+12
.1e-23

0 ({})

: {

car myCar "Saturn" 2 getCar (carTypes("Honda")); 3 special (sales)

js
const sales = "";

function carTypes(name) {
  return name === "" ? name : `${name}`;
}

const car = { myCar: "", getCar: carTypes(""), special: sales };

console.log(car.myCar); // 
console.log(car.getCar); // 
console.log(car.special); // 

js
const car = { manyCars: { a: "", b: "" }, 7: "" };

console.log(car.manyCars.b); // 
console.log(car[7]); // 

JavaScript

(.)

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

([])

js
console.log(unusualPropertyNames[""]); // 
console.log(unusualPropertyNames["!"]); // !

foo: foo super

js
const obj = {
  // __proto__
  __proto__: theProtoObj,
  //  'handler: handler' 
  handler,
  // 
  toString() {
    // 
    return `d ${super.toString()}`;
  },
  // 
  ["prop_" + (() => 42)()]: 42,
};

js
const re = /ab+c/;

0 (") (') ()

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

String String String

String JavaScript String String length

js
// 
console.log("Joyo's cat".length); //  10 

(`) (grave accent)

Perl Python

js
// 
`JavaScript  '\n' `;

// 
`JavaScript 
 
 `;

// 
const name = "Lev",
  time = "today";
`Hello ${name}, how are you ${time}?`;

print print [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 };

print`I need to do:
${todos}
My current progress is: ${progress}
`;

// I need to do:
// - Learn JavaScript
// - Learn Web APIs
// - Set up my website
// - Profit!
// My current progress is: {"javascript":20,"html":50,"css":10}

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

console.log

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

js
"one line \n another line";

JavaScript

\0
\b
\f
\n
\r
\t
\v
\'
\"
\\ (\)
\XXX Latin-1 3 8 XXX 0 377 \251
\xXX Latin-1 2 16 XX 00 FF \xA9
\uXXXX Unicode 4 16 XXXX \u00A9 Unicode
\u{XXXXX} Unicode \u{2F804} Unicode \uD87E\uDC04

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

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

c:\temp

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

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

JavaScript


Web Proxy Viewer  |  New URL  |  Original Page