Record What I Didn't Know About Javascript

Javascript

async/defer

๊ธฐ์กด๋ฐฉ์‹

html์ฝ”๋“œ๋ฅผ parsing htmlํ•˜๋‹ค๊ฐ€ script์ฝ”๋“œ๋ฅผ fetchingํ•ด์•ผํ•˜๋ฉด ์ž ์‹œ parsing html๋ฅผ block์‹œ์ผœ script๋ฅผ fetching, executingํ›„ parsing html์„ ์žฌ๊ฐœ ํ–ˆ๋‹ค. ์ด๋Ÿฐ ๋ฐฉ์‹์€ script ์šฉ๋Ÿ‰์ด ๋„ˆ๋ฌด ํฌ๊ฑฐ๋‚˜, fetching ํ•  js๊ฐ€ ๋งŽ์„ ๊ฒฝ์šฐ page loading์„ ์‚ฌ์šฉ์ž๊ฐ€ ํ•˜์—ผ์—†์ด ๊ธฐ๋‹ค๋ฆฌ๊ธฐ๋งŒ ํ•˜๋Š”๋ฐโ€ฆ

async

<script async src="index.js"></script>

parsing html์„ ํ•˜๋˜์ค‘์— script๋ฅผ fetching ํ•  ๋•Œ, script fetchingํ•˜๋ฉด์„œ parsing html์„ ๋™์‹œ์— ์ˆ˜ํ–‰ํ•˜๊ฒŒ ๋œ๋‹ค. parsing html์ด ์ „๋ถ€ ๋๋‚ฌ์„ ๊ฒฝ์šฐ, executing์„ ์ˆ˜ํ–‰ํ•œ๋‹ค. ์ด ๋ฐฉ์‹์€ ์‚ฌ์šฉ์ž๊ฐ€ ๊ธฐ๋‹ค๋ฆฌ์ง€ ์•Š์•„๋„ ๋˜๋Š” ์žฅ์ ์ด ์žˆ์ง€๋งŒ, html์ด js์— ์˜์กดํ•˜๊ฒŒ ๋  ๊ฒฝ์šฐ ํŽ˜์ด์ง€๊ฐ€ ์ •์ƒ์ž‘๋™ํ•˜์ง€ ์•Š์„ ์ˆ˜ ์žˆ๋‹ค.

defer

<script defer src="index.js"></script>

parsing html์„ ํ•˜๋˜์ค‘ script๋ฅผ fetchingํ•ด์•ผํ•˜๋Š” ๊ฒฝ์šฐ parsing html๊ณผ fetching js๋ฅผ ๋™์‹œ์— ํ•˜๋ฉด์„œ executing์€ ์ž‘์—…์ž๊ฐ€ ๋ช…์‹œํ•œ ์ˆœ์„œ๋Œ€๋กœ ์ž‘์—…์„ ํ•˜๊ฒŒ ๋œ๋‹ค. ๊ฐ€์žฅ ์ด์ƒ์ ์ด๋ฉฐ header์•ˆ์—์„œ fetching js๋ฅผ ํ•˜๊ฒŒ ๋ ๊ฒฝ์šฐ๊ฐ€ ๊ฐ€์žฅ ์ด์ƒ์ ์ด๋‹ค.

use strict

js์†Œ์Šค ๊ฐ€์žฅ ์œ„์—

'use strict';

์ž…๋ ฅํ•˜๊ณ  ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•  ์‹œ ์ดˆ์ฐฝ๊ธฐ flexibleํ•œ ์–ธ์–ด๋ผ๋Š” ๊ฐœ๋…์œผ๋กœ ๋งŒ๋“ค์–ด์ง„ javascript์˜ ์œ„ํ—˜์„ฑ์„ ๋ณด๋‹ค ์•ˆ์ „ํ•˜๊ฒŒ ์ž‘์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.

์˜ˆ๋ฅผ ๋“ค์–ด

variable = 6;
console.log(variable);

์„ ์–ธ๋„ ํ•˜์ง€ ์•Š์€ ๋ณ€์ˆ˜์— ๊ฐ’์„ ํ• ๋‹นํ•˜๊ณ  ์ถœ๋ ฅ๊นŒ์ง€ ํ•ด๋„ ์•„๋ฌด๋Ÿฐ ๋ฌธ์ œ์—†์ด ์ •์ƒ์ž‘๋™ํ•˜๋Š” ๋ง๋„ ์•ˆ๋˜๋Š” ์ผ์€ ์ผ์–ด๋‚˜์ง€ ์•Š๋Š”๋‹ค.

NaN/null/undefined

์œ„ ๊ฐ’๋“ค์€ ์–ผํ•๋ณด๋ฉด ๊ฐ™๋‹ค๊ณ  ์ƒ๊ฐ์ด ๋“ค์ง€๋งŒ ์ „๋ถ€ ๋‹ค๋ฅธ ๊ฐ’์ด๋‹ค.

NaN

Not a Number์˜ ์•ฝ์ž๋กœ ์‚ฌ์šฉ์ž๊ฐ€ number type์˜ ๊ฐ’์„ ๊ธฐ๋Œ€ํ•˜๊ณ  ์‚ฌ์šฉํ• ๋•Œ number type์™ธ์˜ ๊ฐ’์ด ๋ฐ˜ํ™˜๋˜๋ฉด return ๋˜๋Š” ๊ฐ’์ด๋‹ค.

> 18 - "String";
< NaN

null

์„ ์–ธ, ํ• ๋‹น์€ ๋˜์—ˆ์ง€๋งŒ, ์•„๋ฌด๋Ÿฐ ๊ฐ’๋„ ๊ฐ€์ง€์ง€ ์•Š์€ ๋ณ€์ˆ˜๊ฐ’ ์ด๋‹ค.

undefined

์„ ์–ธ์€ ๋˜์—ˆ์ง€๋งŒ, ํ• ๋‹น์ด ์•ˆ ๋œ ๋ณ€์ˆ˜๋ฅผ ์‚ฌ์šฉ ํ•  ๋•Œ ๋ฐ˜ํ™˜๋˜๋Š” ๊ฐ’์ด๋‹ค.

==/===

const coercionaryVariable = true + 4;
console.log(coercionaryVariable);
<5

์œ„์™€ ๊ฐ™์ด javascript๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ ๊ฐ•์ œ์ ์œผ๋กœ ํƒ€์ž…์„ ๋ฐ”๊พผ๋‹ค.

==

๋ฌด์–ธ๊ฐ€๋ฅผ ๋น„๊ต ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” == ์—ฐ์‚ฐ์ž๋Š” ํƒ€์ž…๋ณ€ํ™˜์— ๊ฐ•์ œ์„ฑ์„ ๋ ์šฐ๊ณ  ์žˆ๋‹ค.

===

==์™€ ๋ฐ˜๋Œ€๋˜๋Š” ์„ฑ๊ฒฉ์œผ๋กœ type์ด ๊ฐ™์€์ง€ ๋น„๊ต๋ฅผ ํ•  ๋•Œ ๊ฐ€์žฅ ์ด์ƒ์ ์ธ ์„ ํƒ์ด๋‹ค.

typeof/instanceof

๋ณ€์ˆ˜์˜ ์ž๋ฃŒํ˜•์„ ์•Œ๊ณ  ์‹ถ๋‹ค๋ฉด ์‚ฌ์šฉํ•˜๋Š” ๋ช…๋ น์–ด

typeof

const num = 123;
typeof num
< number

์–ด๋– ํ•œ ์ž๋ฃŒํ˜•์ด๋˜ ์‚ฌ์šฉ๊ฐ€๋Šฅ ํ•˜์ง€๋งŒ null์€ Objectํ˜•์œผ๋กœ ๊ฐ’์ด ๋ฐ˜ํ™˜ ๋˜๋Š” ์ด์ƒํ•œ ์˜ค๋ฅ˜๊ฐ€ ์žˆ๋‹ค.

instanceof

const myCellPhone = {
	brand:'Samsung',
	version:'note20',
	color:'black'
};
console.log(myCellPhone instanceof Object);
< true

์™ผ์ชฝ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์˜ค๋ฅธ์ชฝ ํด๋ž˜์Šค์˜ ์ธ์Šคํ„ด์Šค์ธ์ง€๋ฅผ ์•ˆ๋ ค์ฃผ๋Š” ๋ช…๋ น์–ด

null instanceof Obejct;๋Š” false๋ฅผ ๋ฐ˜ํ™˜

parameter

default parameter

function profile(name, age=24) {
	console.log(name, age)
}
profile(์ตœ์˜์„ฑ);

ํŒŒ๋ผ๋ฏธํ„ฐ์˜ ๊ฐ’์„ default๋กœ ์ง€์ •ํ•ด๋ฒ„๋ ค์„œ undefined๊ฐ€ ๋ฐ˜ํ™˜๋˜์ง€ ์•Š๋„๋ก ๋ฐฉ์ง€

rest parameter

function skillStacks(...args) {
	for(let i = 0; i < args.length; i++) {
		console.log(args[i]);
	}
}

skillStacks("Spring", "React.js", "Javascript", "java", "node.js", "React Native");

parameter๊ฐ’์„ ๋ฐฐ์—ด๋กœ ๋ฐ›๋Š”๋‹ค๋Š” ๊ฒƒ์„ ๋ช…์‹œํ•œ๋‹ค.

for

args.forEach((arg) => console.log(arg));

๋ฐฐ์—ด์˜ ๊ฐ’์„ ํ•˜๋‚˜์”ฉ ๋ฝ‘์•„์˜ด

for(cosnt arg of args) {
	console.log(arg);
}

์œ„ ๊ธฐ๋Šฅ๊ณผ ๋™์ผ

hoisting

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋Š” hoisting์ด๋ผ๋Š” ๊ฐœ๋…์„ ์ง€์›ํ•œ๋‹ค.

log();
const log = function () {
	console.log("writing...");
}

varํƒ€์ž… ๋ณ€์ˆ˜, function์„ ์„ ์–ธ, ํ• ๋‹น ์ „์— ํ˜ธ์ถœํ•ด๋„ ์‹คํ–‰์ด ๊ฐ€๋Šฅํ•œ๋ฐ javascript๊ฐ€ ์•ž์—์„œ ๋งํ•œ ๋‘๊ฐ€์ง€๋ฅผ ๊ฐ€์žฅ ์œ„์— ์ž๋™์œผ๋กœ ์˜ฌ๋ ค์ค€๋‹ค.

callback function

console.log(1);
setTimeout(() => console.log(2), 1000);
console.log(3);

image-20211021235008706

javascript๋Š” stack์—์„œ ํ•จ์ˆ˜๋ฅผ ํ˜ธ์ถœํ•œ๋‹ค. ๋งŒ์•ฝ setTimeout๊ฐ™์€ API๋ฅผ ํ˜ธ์ถœ ํ•  ๊ฒฝ์šฐ webAPI์— ๋ฌด์กฐ๊ฑด ๊ฐ€๊ฒŒ ๋˜์–ด์„œ Message Queue๋ฅผ ๊ฑฐ์ฒ˜ stack์— ๋‹ค์‹œ ๊ฐ€๊ฒŒ ๋˜๋Š”๋ฐ stack์ด ๋น„์–ด์žˆ์–ด์•ผ ์ถœ๋ ฅ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ž˜์„œ ์ถœ๋ ฅ์ˆœ์„œ๋Š” 1,3,2์ด๋‹ค.

??/|| ์—ฐ์‚ฐ์ž

??

function printMessage(text) {
	console.log(text ?? "Nothing to display");
}

printMessage("text");
printMessage(null);
printMessage(undefined);

< text
<"Nothing to display"
<"Nothing to display"

Nullish coalescing ์—ฐ์‚ฐ์ž๋Š” ์—ฐ์‚ฐ์ž ๊ธฐ์ค€ ์™ผ์ชฝ์˜ ๋ฌธ์ž์—ด์ด null, undefined์ผ ๊ฒฝ์šฐ ์™ผ์ชฝ์˜ ๋ฌธ์ž์—ด์„ ๋Œ€์‹  ๋ฐ˜ํ™˜

||

Logical Or ์—ฐ์‚ฐ์ž๋Š” ์—ฐ์‚ฐ์ž ๊ธฐ์ค€ ์™ผ์ชฝ์˜ ๋ฌธ์ž์—ด์ด NaN, -1, 0, false, undefined, null๊ฐ’ ์ผ๊ฒฝ์šฐ์— ์˜ค๋ฅธ์ชฝ ๋ฌธ์ž์—ด์„ ๋ฐ˜ํ™˜

default parameter์™€ ๋‹ค๋ฅธ์ ์€ undefined์ผ ๊ฒฝ์šฐ์—๋งŒ ์ž…๋ ฅํ•ด๋…ผ default๊ฐ’์ด ์ถœ๋ ฅ๋จ