ジュニアのJavaScript開発者のための18のヒント/トリック
23850 ワード
1 .文字列に変換する
const input = 123;
console.log(input + ''); // '123'
console.log(String(input)); // '123'
console.log(input.toString()); // '123'
2 .数値への変換
const input = '123';
console.log(+input); // 123
console.log(Number(input)); // 123
console.log(parseInt(input)); // 123
booleanへの変換
const input = 1;
// Solution 1 - Use double-exclamation (!!) to convert to boolean
console.log(!!input); // true
// Solution 2 - Pass the value to Boolean()
console.log(Boolean(input)); // true
4 .文字列の問題
'false'
const value = 'false';
console.log(Boolean(value)); // true
console.log(!!value); // true
// The best way to check would be,
console.log(value === 'false');
5 . NULLと未定義
null
が値undefined
がない.null
は、空の箱のようですundefined
全く箱がありません.→
const fn = (x = 'default value') => console.log(x);
fn(undefined); // default value
fn(); // default value
fn(null); // null
時null
が渡されると、デフォルト値は取られず、undefined
または何も渡されません.真実と虚偽の値
虚偽値
false
, 0
, ""
(空の文字列)、null
, undefined
, & NaN
.真の価値
"false"
, "0"
, {}
(空のオブジェクト)[]
(空の配列)7 .どのような変更を行うことができます
const
const
値が変更されない場合に使用されます.EXconst name = 'Codedrops';
name = 'Codedrops.tech'; // Error
const list = [];
list = [1]; // Error
const obj = {};
obj = { name: 'Codedrops' }; // Error
しかし、以前に割り当てられた配列/オブジェクト参照で値を更新するのに使用できますconst list = [];
list.push(1); // Works
list[0] = 2; // Works
const obj = {};
obj['name'] = 'Codedrops'; // Works
二等分三倍の差
// Double equal - Converts both the operands to the same type and then compares
console.log(0 == '0'); // true
// Triple equal - Does not convert to same type
console.log(0 === '0'); // false
9 .引数を受け入れるより良い方法
function downloadData(url, resourceId, searchText, pageNo, limit) {}
downloadData(...); // need to remember the order
より簡単な方法-function downloadData(
{ url, resourceId, searchText, pageNo, limit } = {}
) {}
downloadData(
{ resourceId: 2, url: "/posts", searchText: "programming" }
);
通常の関数を矢印関数として書き換える
const func = function() {
console.log('a');
return 5;
};
func();
書き換えることができますconst func = () => (console.log('a'), 5);
func();
11 .矢印関数からオブジェクト/式を返す
const getState = (name) => ({name, message: 'Hi'});
setを配列に変換する
const set = new Set([1, 2, 1, 4, 5, 6, 7, 1, 2, 4]);
console.log(set); // Set(6) {1, 2, 4, 5, 6, 7}
set.map((num) => num * num); // TypeError: set.map is not a function
配列に変換するにはconst arr = [...set];
13 .値が配列かどうかを調べる
const arr = [1, 2, 3];
console.log(typeof arr); // object
console.log(Array.isArray(arr)); // true
14 .オブジェクトキーを挿入順に格納する
const obj = {
name: "Human",
age: 0,
address: "Earth",
profession: "Coder",
};
console.log(Object.keys(obj)); // name, age, address, profession
Objects
キーが作成された順序を維持します.15 .不確かな合体演算子
const height = 0;
console.log(height || 100); // 100
console.log(height ?? 100); // 0
Nullish coalescing operator
(?)左側の値がundefined
or null
16章MAP ()
これは、配列のすべての要素に関数を適用するのに役立つユーティリティ関数です.
この関数は、適用された関数から返される値を含む新しい配列を返します.→
const numList = [1, 2, 3];
const square = (num) => {
return num * num
}
const squares = numList.map(square);
console.log(squares); // [1, 4, 9]
ここでは、関数square
がすべての要素に適用されます.すなわち、1、2、3.関数の戻り値は、新しい要素値として返されます.
17 .試してみてください.キャッチ.実際の例
const getData = async () => {
try {
setLoading(true);
const response = await fetch(
"https://jsonplaceholder.typicode.com/posts"
);
// if error occurs here, then all the statements
//in the try block below this wont run.
// Hence cannot turn off loading here.
const data = await response.json();
setData(data);
} catch (error) {
console.log(error);
setToastMessage(error);
} finally {
setLoading(false); // Turn off loading irrespective of the status.
}
};
getData();
18 .破壊
const response = {
msg: "success",
tags: ["programming", "javascript", "computer"],
body: {
count: 5
},
};
const {
body: {
count,
unknownProperty = 'test'
},
} = response;
console.log(count, unknownProperty); // 5 'test'
読書ありがとう💙@ codedropに続いてください.毎日のポストのための技術.
マイクロラーニング● Web開発● ジャバスクリプト● メルンスタック● ジャバスクリプト
codedrops.tech
Reference
この問題について(ジュニアのJavaScript開発者のための18のヒント/トリック), 我々は、より多くの情報をここで見つけました https://dev.to/ml318097/18-tips-tricks-for-junior-javascript-developer-57oaテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol