| [ Web Proxy ] |
| Viewing: https://developer.mozilla.org/ja/docs/Web/JavaScript/Guide/Language_overview | [Back] [Original] |
Get to know MDN better
JavaScript JavaScript
JavaScript
JavaScript 2 (Number) (BigInt)
IEEE 754 64 -(253 1) 253 1 1.79 10308 JavaScript
console.log(3 / 2); // 1.5, 1
IEEE 754
console.log(0.1 + 0.2); // 0.30000000000000004
32
console.log(0b111110111); // 503
console.log(0o767); // 503
console.log(0x1f7); // 503
console.log(5.03e2); // 503
console.log(-3n / 2n); // -1n
Math.sin(3.5);
const circumference = 2 * Math.PI * r;
3
+ Number()
NaN "Not a Number" Infinity Math.log() NaN Infinity
NaN NaN NaN JavaScript IEEE 754
JavaScript Unicode UTF-16
console.log("Hello, world");
console.log(""); // Unicode
JavaScript
console.log("Hello"[1] === "e"); // true
const age = 25;
console.log(" " + age + " "); //
console.log(` ${age} `); //
JavaScript nullnull undefined undefined
return (return;) undefined obj.iDontExist) undefined let x;) undefined JavaScript true false
false0 ("")NaNnullundefined false true Boolean(""); // false
Boolean(234); // true
JavaScript if " (truthy)" " (falsy)" true false
(Symbol) Symbol()
let
let a;
let name = "Simon";
// myLetVariable **
for (let myLetVariable = 0; myLetVariable < 5; myLetVariable++) {
// myLetVariable
}
// myLetVariable **
const
const Pi = 3.14; // Pi
console.log(Pi); // 3.14
const
const Pi = 3.14;
Pi = 1; //
const
const obj = {};
obj.a = 1; //
console.log(obj); // { a: 1 }
var JavaScript
undefined const
function foo(x, condition) {
if (condition) {
console.log(x);
const x = 2;
console.log(x);
}
}
foo(1, true);
const x = 2 x x "1" "2" JavaScript console.log "Cannot access 'x' before initialization" let
let a = 1;
a = "foo";
JavaScript +-*/ % () = += -= x = x y
x += 5;
x = x + 5;
++ --
"hello" + " world"; // "hello world"
"3" + 4 + 5; // "345"
3 + 4 + "5"; // "75"
123 == "123"; // true
1 == true; // true
123 === "123"; // false
1 === true; // false
!= !==
const a = 0 && "Hello"; // 0 0
const b = "Hello" || "world"; // "Hello" "world" "Hello"
&& || 2 null
const name = o && o.getName();
const name = cachedName || (cachedName = getName());
JavaScript C
JavaScript C if else
let name = "kittens";
if (name === "puppies") {
name += " woof";
} else if (name === "kittens") {
name += " meow";
} else {
name += "!";
}
name === "kittens meow";
JavaScript elif else if if else
JavaScript while do...while 1
while (true) {
// !
}
let input;
do {
input = get_input();
} while (inputIsNotValid(input));
JavaScript for C Java 1
for (let i = 0; i < 5; i++) {
// 5
}
JavaScript for 2 for...of for...in
for (const value of array) {
//
}
for (const property in object) {
//
}
switch
switch (action) {
case "draw":
drawIt();
break;
case "eat":
eatIt();
break;
default:
doNothing();
}
Rust JavaScript const a = if (x) { 1 } else { 2 }
JavaScript try...catch
try {
buildMySite("./website");
} catch (e) {
console.error(":", e);
}
function buildMySite(siteDirectory) {
if (!pathExists(siteDirectory)) {
throw new Error("");
}
}
throw Error Error TypeError RangeError JavaScript catch 1 catch instanceof throw
try {
buildMySite("./website");
} catch (e) {
if (e instanceof RangeError) {
console.error(":", e);
console.log("...");
buildMySite("./website");
} else {
//
// throw
throw e;
}
}
try...catch
JavaScript
2
const obj = {
name: "Carrot",
for: "Max",
details: {
color: "orange",
size: 12,
},
};
//
obj.name = "Simon";
const name = obj.name;
//
obj["name"] = "Simon";
const name = obj["name"];
//
const userName = prompt("");
obj[userName] = prompt("");
obj.details.color; // orange
obj["details"]["size"]; // 12
const obj = {};
function doSomething(o) {
o.x = 1;
}
doSomething(obj);
console.log(obj.x); // 1
2 (!==) 2
const me = {};
const stillMe = me;
me.x = 1;
console.log(stillMe.x); // 1
JavaScript [] length 1
const a = ["dog", "cat", "hen"];
a.length; // 3
JavaScript length
const a = ["dog", "cat", "hen"];
a[100] = "fox";
console.log(a.length); // 101
console.log(a); // ['dog', 'cat', 'hen', 97, 'fox']
undefined
const a = ["dog", "cat", "hen"];
console.log(typeof a[90]); // undefined
const arr = [1, "foo", true];
arr.push({});
// arr = [1, "foo", true, {}]
C for
for (let i = 0; i < a.length; i++) {
// a[i]
}
C++/Java for (int x : arr) for...of
for (const currentValue of a) {
// currentValue ()
}
const babies = ["dog", "cat", "hen"].map((name) => `baby ${name}`);
// babies = ['baby dog', 'baby cat', 'baby hen']
JavaScript
function add(x, y) {
const total = x + y;
return total;
}
JavaScript 0 return return return JavaScript undefined
undefined
add(); // NaN
// Equivalent to add(undefined, undefined)
add(2, 3, 4); // 5
// 1 2 4
function avg(...args) {
let sum = 0;
for (const item of args) {
sum += item;
}
return sum / args.length;
}
avg(2, 3, 4, 5); // 3.5
args
function avg(firstValue, ...args) firstValue args
// { }
function area({ width, height }) {
return width * height;
}
// { }
console.log(area({ width: 2, height: 3 }));
function avg(firstValue, secondValue, thirdValue = 0) {
return (firstValue + secondValue + thirdValue) / 3;
}
avg(1, 2); // 1, instead of NaN
JavaScript
//
const avg = function (...args) {
let sum = 0;
for (const item of args) {
sum += item;
}
return sum / args.length;
};
avg() function avg() {}
//
const avg = (...args) => {
let sum = 0;
for (const item of args) {
sum += item;
}
return sum / args.length;
};
// `return`
const sum = (a, b, c) => a + b + c;
IIFE (Immediately invoked function expression)
(function () {
//
})();
JavaScript DOM
function countChars(elm) {
if (elm.nodeType === 3) {
// TEXT_NODE
return elm.nodeValue.length;
}
let count = 0;
for (let i = 0, child; (child = elm.childNodes[i]); i++) {
count += countChars(child);
}
return count;
}
const charsInBody = (function counter(elm) {
if (elm.nodeType === 3) {
// TEXT_NODE
return elm.nodeValue.length;
}
let count = 0;
for (let i = 0, child; (child = elm.childNodes[i]); i++) {
count += counter(child);
}
return count;
})(document.body);
JavaScript JavaScriptCoreSafari
//
const add = (x) => (y) => x + y;
//
const babies = ["dog", "cat", "hen"].map((name) => `baby ${name}`);
JavaScript JavaScript
JavaScript JavaScript
function parentFunc() {
const a = 1;
function nestedFunc() {
const b = 4; // parentFunc
return a + b;
}
return nestedFunc(); // 5
}
1 2
JavaScript class Java
class Person {
constructor(name) {
this.name = name;
}
sayHello() {
return `Hello, I'm ${this.name}!`;
}
}
const p = new Person("Maria");
console.log(p.sayHello());
JavaScript new
const withAuthentication = (cls) =>
class extends cls {
authenticate() {
//
}
};
class Admin extends withAuthentication(Person) {
//
}
static # private # Python _
JavaScript 3
JavaScript
//
fs.readFile(filename, (err, content) => {
//
if (err) {
throw err;
}
console.log(content);
});
//
//
fs.readFile(filename)
.then((content) => {
//
console.log(content);
})
.catch((err) => {
throw err;
});
//
// Async/await
async function readFile(filename) {
const content = await fs.readFile(filename);
console.log(content);
}
then() await then() Promise<Promise<T>>
import { foo } from "./foo.js";
//
const b = 2;
export const a = 1;
HaskellPythonJava JavaScript URL
JavaScript Math Intl JavaScript
JavaScript API console.log() JavaScript
JavaScript JavaScript DOM API API Node.js JavaScript JavaScript API
JavaScript JavaScript JavaScript
| Web Proxy Viewer | New URL | Original Page |