首頁 > web前端 > js教程 > 主體

JavaScript 備忘錄

王林
發布: 2024-08-08 03:41:12
原創
1196 人瀏覽過

JavaScript Cheat Sheets

Hey everyone! I've got some great news for all you beginners learning JavaScript! A quick reference guide for JavaScript is now available! You can easily check important concepts, functions, and methods right away. For more details about the guide, check it out here.

What's in the Quick Reference Guide

Console

// => Hello world!
console.log("Hello world!");

// => Hello CheatSheets.zip
console.warn("hello %s", "CheatSheets.zip");

// Output error message to standard error
console.error(new Error("Oops!"));
登入後複製

Numbers

let amount = 6;
let price = 4.99;
登入後複製

Variables

let x = null;
let name = "Tammy";
const found = false;

// => Tammy, false, null
console.log(name, found, x);

var a;
console.log(a); // => undefined
登入後複製

Strings

let single = "Wheres my bandit hat?";
let double = "Wheres my bandit hat?";

// => 21
console.log(single.length);
登入後複製

Arithmetic Operators

5 + 5 = 10     // Addition
10 - 5 = 5     // Subtraction
5 * 10 = 50    // Multiplication
10 / 5 = 2     // Division
10 % 5 = 0     // Modulus
登入後複製

Comments

// This is a comment

/*
You need to change this setting
before deployment.
*/
登入後複製

Assignment Operators

let number = 100;

// Both statements add 10
number = number + 10;
number += 10;

console.log(number);
// => 120
登入後複製

String Interpolation

let age = 7;

// String concatenation
"Tommy is " + age + " years old.";

// String interpolation
`Tommy is ${age} years old.`;
登入後複製

Conditional Statements

const isMailSent = true;

if (isMailSent) {
  console.log("Mail sent to recipient");
}
登入後複製

Ternary Operator

var x = 1;

// => true
result = x == 1 ? true : false;
登入後複製

Logical Operators

true || false; // true
10 > 5 || 10 > 20; // true
false || false; // false
10 > 100 || 10 > 20; // false
登入後複製

Comparison Operators

1 > 3; // false
3 > 1; // true
250 >= 250; // true
1 === 1; // true
1 === 2; // false
1 === "1"; // false
登入後複製

Arrays

const fruits = ["apple", "orange", "banana"];

// Different data types
const data = [1, "chicken", false];
登入後複製

Objects

const apple = {
  color: "Green",
  price: { bulk: "$3/kg", smallQty: "$4/kg" },
};
console.log(apple.color); // => Green
console.log(apple.price.bulk); // => $3/kg
登入後複製

Classes

class Dog {
  constructor(name) {
    this._name = name;
  }

  introduce() {
    console.log("This is " + this._name + " !");
  }

  // Static method
  static bark() {
    console.log("Woof!");
  }
}

const myDog = new Dog("Buster");
myDog.introduce();

// Calling static method
Dog.bark();
登入後複製

Modules

// myMath.js

// Default export
export default function add(x, y) {
  return x + y;
}

// Named export
export function subtract(x, y) {
  return x - y;
}

// Multiple exports
function multiply(x, y) {
  return x * y;
}
function duplicate(x) {
  return x * 2;
}
export { multiply, duplicate };
登入後複製

Asynchronous Processing

function helloWorld() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve("Hello World!");
    }, 2000);
  });
}

async function msg() {
  const msg = await helloWorld();
  console.log("Message:", msg);
}

msg(); // Message: Hello World! <-- after 2 seconds
登入後複製

This quick reference guide covers a wide range of topics from the basics to more advanced JavaScript concepts. Be sure to download it here and make it a handy tool for your coding journey!

以上是JavaScript 備忘錄的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!