より少ないJavascriptを書く9つのトリック.


ハイファイ👋, 私のブログを開いてくれてありがとう.私は、あなたがよくやっていて、より少ないJavascriptを書く若干のトリックを学ぶ準備ができていることを望みます😎.
では、始めましょう!
変数の宣言
//Longhand
let x;
let y;
let z = "post";

//Shorthand
let x, y, z = "post";
代入演算子
//Longhand
x = x + y;
x = x - y;

//Shorthand
x += y;
x -= y;
三項演算子
let answer, num = 15;

//Longhand
if (num > 10) {
  answer = "greater than 10";
} 
else {
  answer = "less than 10";
}

//Shorthand
const answer = num > 10 ? "greater than 10" : "less than 10";
ループ用ショート
const languages = ["html", "css", "js"];

//Longhand
for (let i = 0; i < languages.length; i++) {
  const language = languages[i];
  console.log(language);
}

//Shorthand
for (let language of languages) console.log(language);
テンプレートリテラル
const name = "Dev";
const timeOfDay = "afternoon";

//Longhand
const greeting = "Hello " + name + ", I wish you a good " + timeOfDay + "!";

//Shorthand
const greeting = `Hello ${name}, I wish you a good ${timeOfDay}!`;
6 .関数
//Longhand
function sayHello(name) {
  console.log("Hello", name);
}

list.forEach(function (item) {
  console.log(item);
});

//Shorthand
sayHello = name => console.log("Hello", name);

list.forEach(item => console.log(item));
7 .オブジェクト配列表記法
//Longhand
let arr = new Array();
arr[0] = "html";
arr[1] = "css";
arr[2] = "js";

//Shorthand
let arr = ["html", "css", "js"];
オブジェクトの破壊
const post = {
  data: {
    id: 1,
    title: "9 trick to write less Javascript",
    text: "Hello World!",
    author: "Shoaib Sayyed",
  },
};

//Longhand
const id = post.data.id;
const title = post.data.title;
const text = post.data.text;
const author = post.data.author;

//Shorthand
const { id, title, text, author } = post.data;
9 .同一のキーと値を持つオブジェクト
//Longhand
const userDetails = {
  name: name, // 'name' key = 'name' variable
  email: email,
  age: age,
  location: location,
};

//Shorthand
const userDetails = { name, email, age, location };
それで😎.
読書ありがとう!私の名前はShoaib sayyedです;私は人々が新しいスキルを学ぶのを助ける😊. あなたが新しい記事やリソースについて通知されたい場合は、私に従うことができます.