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

JavaScript - JavaScript | MDN

This page was translated from English by the community. Learn more and join the MDN Web Docs community.

View in English Always switch to English

JavaScript

JavaScript , . Java C . JavaScript . JavaScript . first-class .

C Java , JavaScript .

In this article

. JavaScript , . JavaScript 7 .

. .

JavaScript . .

(Numbers)

JavaScript Number BigInt .

64 (IEEE 754) , , -(253 1) 253 1 . 1.79 10308 . JavaScript .

js
console.log(3 / 2); // 1.5, not 1

" " " float()". IEEE 754 .

js
console.log(0.1 + 0.2); // 0.30000000000000004

, , 32 .

Number (literals) (2, 8, 10 16) .

js
console.log(0b111110111); // 503
console.log(0o767); // 503
console.log(0x1f7); // 503
console.log(5.03e2); // 503

BigInt . C (: 0 ) , . BigInt n .

js
console.log(-3n / 2n); // -1n

, , . BigInt .

Math .

js
Math.sin(3.5);
const circumference = 2 * Math.PI * r;

.

+ Number() .

NaN("Not a Number" ) Infinity . " " NaN . , Math.log() . 0 'Infinity'( ) .

NaN . NaN . NaN JavaScript (IEEE 754 ).

(Strings)

JavaScript . . UTF-16 encoded .

js
console.log("Hello, world");
console.log(""); //           .

. JavaScript . , .

js
console.log("Hello"[1] === "e"); // true

( ) , length .

. , .

+ . , . . Python f- C# , ( ) .

js
const age = 25;
console.log("I am " + age + " years old."); //  
console.log(`I am ${age} years old.`); //  

JavaScript ( null ) null undefined . undefined .

  • return (return;) undefined .
  • object (obj.iDontExist) undefined .
  • (let x;) undefined .

JavaScript true false (Boolean) . . .

  1. false, 0, (""),NaN, null, undefined false .
  2. true .

Boolean() .

js
Boolean(""); // false
Boolean(234); // true

. JavaScript if ( ) . true, false " (truthy)" " (falsy)" .

&& (), || (), ! () . .

(Symbol) . Symbol() (Symbol) . , "" . .

(Variables)

JavaScript let, const var .

let . " " .

js
let a;
let name = "Simon";

// myLetVariable   **

for (let myLetVariable = 0; myLetVariable < 5; myLetVariable++) {
  // myLetVariable    
}

// myLetVariable   **

const . " " .

js
const Pi = 3.14; //  Pi 
console.log(Pi); // 3.14

const .

js
const Pi = 3.14;
Pi = 1; //        .

const . .

js
const obj = {};
obj.a = 1; //  
console.log(obj); // { a: 1 }

var (: ) , JavaScript .

, undefined. , const .

let const , (temporal dead zone) . .

js
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 " 'x' ." . let .

JavaScript . ( ) . let .

js
let a = 1;
a = "foo";

JavaScript +, -, *, /, %( ), **( ) . = , += -= . x = x y .

js
x += 5;
x = x + 5;

++ -- . .

+ .

js
"hello" + " world"; // "hello world"

( ) . .

js
"3" + 4 + 5; // "345"
3 + 4 + "5"; // "75"

.

JavaScript <, >, <= >= , . , . , .

js
123 == "123"; // true
1 == true; // true

123 === "123"; // false
1 === true; // false

!= !== .

JavaScript . "" .

js
const a = 0 && "Hello"; // 0 "falsy" , 0.
const b = "Hello" || "world"; // "Hello" "world"  "truthy", "Hello" .

&& || . , . null .

js
const name = o && o.getName();

( )

js
const name = cachedName || (cachedName = getName());

. .

JavaScript C . .

  • , .
  • // /* */ . # Perl, Python, and Bash .
  • JavaScript . . Python .

JavaScript .

JavaScript C . if else , .

js
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 . , .

js
while (true) {
  // !
}

let input;
do {
  input = get_input();
} while (inputIsNotValid(input));

JavaScript for C Java . , .

js
for (let i = 0; i < 5; i++) {
  //   5 
}

JavaScript for . iterables( ) for...of, enumerable for...in.

js
for (const value of array) {
  // value  .
}

for (const property in object) {
  //    .
}

switch .

js
switch (action) {
  case "draw":
    drawIt();
    break;
  case "eat":
    eatIt();
    break;
  default:
    doNothing();
}

C , case labels , break "". . , case , . === .

Rust , JavaScript . , const a = if (x) { 1 } else { 2 } .

JavaScript try...catch .

js
try {
  buildMySite("./website");
} catch (e) {
  console.error("Building site failed:", e);
}

(Error) throw . .

js
function buildMySite(siteDirectory) {
  if (!pathExists(siteDirectory)) {
    throw new Error("Site directory does not exist");
  }
}

, . throw . Error . TypeError RangeError Error , . JavaScript . , instanceof , case .

js
try {
  buildMySite("./website");
} catch (e) {
  if (e instanceof RangeError) {
    console.error("Seems like a parameter is out of range:", e);
    console.log("Retrying...");
    buildMySite("./website");
  } else {
    //      .
    //          .
    throw e;
  }
}

try...catch , .

.

(Objects)

JavaScript - (name-value pairs) . JavaScript .

  • Python Dictionaries.
  • Perl Ruby Hashes.
  • C C++ Hash tables.
  • Java HashMaps.
  • PHP (Associative arrays).

JavaScript (hashes). , JavaScript . , , , . strings (symbols). .

.

js
const obj = {
  name: "Carrot",
  for: "Max",
  details: {
    color: "orange",
    size: 12,
  },
};

(.) ([]) . , . .

js
//  
obj.name = "Simon";
const name = obj.name;

//  
obj["name"] = "Simon";
const name = obj["name"];

//      .
const userName = prompt("what is your key?");
obj[userName] = prompt("what is its value?");

.

js
obj.details.color; // orange
obj["details"]["size"]; // 12

, , .

js
const obj = {};
function doSomething(o) {
  o.x = 1;
}
doSomething(obj);
console.log(obj.x); // 1

(!==) . , .

js
const me = {};
const stillMe = me;
me.x = 1;
console.log(stillMe.x); // 1

, . .

( ) , . .

(Arrays)

JavaScript . ( [] ) , length . .

.

js
const a = ["dog", "cat", "hen"];
a.length; // 3

JavaScript . . "" length .

js
const a = ["dog", "cat", "hen"];
a[100] = "fox";
console.log(a.length); // 101
console.log(a); // ['dog', 'cat', 'hen', empty  97, 'fox']

(sparse array) . . !

. undefined .

js
const a = ["dog", "cat", "hen"];
console.log(typeof a[90]); // undefined

.

js
const arr = [1, "foo", true];
arr.push({});
// arr = [1, "foo", true, {}]

C for .

js
for (let i = 0; i < a.length; i++) {
  // a[i]   
}

, C++/Java for (int x : arr) for...of .

js
for (const currentValue of a) {
  // currentValue   
}

. . , map() .

js
const babies = ["dog", "cat", "hen"].map((name) => `baby ${name}`);
// babies = ['baby dog', 'baby cat', 'baby hen']

(Functions)

, JavaScript . .

js
function add(x, y) {
  const total = x + y;
  return total;
}

JavaScript 0 . . return . ( ), JavaScript undefined .

. undefined . .

js
add(); // NaN
// add(undefined, undefined) .

add(2, 3, 4); // 5
//    . 4 .

. , rest Python *args (JS , **kwargs ).

js
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 .

(rest parameter) , . , function avg(firstValue, ...args) firstValue args .

, (spread syntax) spread . , avg(...numbers)

JavaScript . , .

js
// ({ }) .  .
function area({ width, height }) {
  return width * height;
}

//  ({ })   .
console.log(area({ width: 2, height: 3 }));

, ( undefined ) .

js
function avg(firstValue, secondValue, thirdValue = 0) {
  return (firstValue + secondValue + thirdValue) / 3;
}

avg(1, 2); // NaN , 1.

JavaScript ( ) . , .

js
//     .
const avg = function (...args) {
  let sum = 0;
  for (const item of args) {
    sum += item;
  }
  return sum / args.length;
};

avg() . , function avg() {} .

.

js
//     .
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 Expressions) .

js
(function () {
  // 
})();

IIFE .

JavaScript . DOM .

js
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;
}

, .

js
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 . (tail-call optimization) , JavaScriptCore (Safari ) . , .

(first-class objects)

JavaScript (first-class objects). , . , JavaScript (closures) .

js
//   
const add = (x) => (y) => x + y;
//    
const babies = ["dog", "cat", "hen"].map((name) => `baby ${name}`);

JavaScript JavaScript , .

(Inner functions)

JavaScript . JavaScript .

js
function parentFunc() {
  const a = 1;

  function nestedFunc() {
    const b = 4; // parentFunc    
    return a + b;
  }
  return nestedFunc(); // 5
}

. , . .

. , . .

JavaScript Java class .

js
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 . , . . , , . . , (mixin) .

js
const withAuthentication = (cls) =>
  class extends cls {
    authenticate() {
      // 
    }
  };

class Admin extends withAuthentication(Person) {
  // 
}

static . Private hash(#) (private ) . . (Python # _ .) , Private . (derived classes) .

guide page .

JavaScript . , . , (polling) .

JavaScript .

, JavaScript .

js
//  (Callback-based)
fs.readFile(filename, (err, content) => {
  //           .
  if (err) {
    throw err;
  }
  console.log(content);
});
//       .

//  (Promise-based)
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);
}

, , . , . , , .

, . , (Promise) , then() . , await , () . Promise "" . Promise . . , Promise then() (monads) (, . , Promise<Promise<T>> ).

, Non-Blocking IO Node.js , . , JavaScript CPU ( ) . workers .

, promises JavaScript .

JavaScript . URL . import export .

js
import { foo } from "./foo.js";

// export         .
const b = 2;

export const a = 1;

Haskell, Python, Java JavaScript . URL , "" .

, JavaScript . Math Intl . JavaScript , .

. , Node.js npm , , Deno URL HTTP URL .

.

, " " " " .

JavaScript . . . , API( console.log()) , JavaScript .

JavaScript () , . , , , . JavaScript (DOM API ), Node.js( API ) . JavaScript ( ) , , , , , . JavaScript , . , API .

JavaScript . , JavaScript JavaScript .

, .


Web Proxy Viewer  |  New URL  |  Original Page