๐ŸŒฑ → ๐ŸŒณ

[JavaScript] JS ๊ธฐ์ดˆ ๋ณธ๋ฌธ

Client/Javascript

[JavaScript] JS ๊ธฐ์ดˆ

BAY 2022. 7. 13. 14:52
728x90

๐Ÿ“Œ ๋ณ€์ˆ˜๋ช… ๊ธฐ๋ณธ ๊ทœ์น™ 

  • ๋ณ€์ˆ˜ ์ด๋ฆ„์œผ๋กœ๋Š” ๋ฌธ์ž/์ˆซ์ž/_/$ ๋งŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅ 
  • ์ฒซ ๊ธ€์ž๋Š” ์ˆซ์ž ๋ถˆ๊ฐ€
  • ์˜ˆ์•ฝ์–ด ์‚ฌ์šฉ ๋ถˆ๊ฐ€ 
  • ์˜๋ฏธ๋ฅผ ๋‚ดํฌ ํ•˜๋Š” ๋ณ€์ˆ˜ ์ด๋ฆ„์ด ์ข‹์Œ 
    • ์•ž ๋‹จ์–ด๊ฐ€ ๋ช…์‚ฌ -> ๋ณ€์ˆ˜
    • ์•ž ๋‹จ์–ด๊ฐ€ be๋™์‚ฌ -> boolean
    • ์•ž ๋‹จ์–ด๊ฐ€ ๋™์‚ฌ -> ํ•จ์ˆ˜
  • ์ƒ์ˆ˜, ๊ณ ์ •๊ฐ’์€ ์ „์ฒด ๋Œ€๋ฌธ์ž๋กœ ์„ ์–ธ

 

๐Ÿ“Œ ๋ฐ์ดํ„ฐ ํƒ€์ž…

: string, number, boolean, undefined, null, object, array ๋“ฑ 

 

โœ… typeof : ํ•ด๋‹น ์ž๋ฃŒ(๋ณ€์ˆ˜, ๋ฐ์ดํ„ฐ ๋“ฑ)์˜ ๋ฐ์ดํ„ฐ ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•ด์คŒ 

  • ์ฃผ์˜, object์•ˆ์˜ ์ž๋ฃŒ์™€ null๋Š” object๋ผ๊ณ  ๋œธ
console.log(typeof null);
console.log(typeof "hi");
console.log(typeof 77);
// {} ๋นˆ object ์„ ์–ธ
console.log(typeof {});

console ํ™•์ธ

โœ… ํ˜• ๋ณ€ํ™˜

prompt : ์ž…๋ ฅ ๋ฐ›๋Š” ํ•จ์ˆ˜, string์œผ๋กœ ์ €์žฅ ๋จ

๋ฌธ์ž๋กœ ๋ณ€ํ™˜ -> String();

์ˆซ์ž๋กœ ๋ณ€ํ™˜ -> Number();

let mathScore = prompt("์ˆ˜ํ•™์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
let engScore = prompt("์˜์–ด์ ์ˆ˜๋ฅผ ์ž…๋ ฅํ•ด ์ฃผ์„ธ์š”.");
mathScore = Number(mathScore);
engScore = Number(engScore);
let avgScore = (mathScore + engScore) / 2;
console.log(avgScore);
console.log(Number("abcdefg"));
// NaN(Not a Number) ๋ฐ˜ํ™˜
console.log(Boolean("hi"));
// true ๋ฐ˜ํ™˜ 
// ์•„์Šคํ‚ค์ฝ”๋“œ๋กœ ๋ณ€ํ™˜ํ•˜๋ฉด 1์ด์ƒ์˜ ํŠน์ • ์ˆซ์ž๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— true ๋ฐ˜ํ™˜

 

๐Ÿ“Œ ๋ฐฑํ‹ฑ(``) ๋ฌธ์ž

for(let i = 0; i < nameArr.length; i++) {
    console.log(`${i+1} ๋ฒˆ์งธ ์ด๋ฆ„์€ ${nameArr[i]} ์ž…๋‹ˆ๋‹ค`);
}
  • ๋ฌธ์ž์™€ ๋ณ€์ˆ˜ ํ˜ผ์šฉํ•˜์—ฌ ์ถœ๋ ฅ ๊ฐ€๋Šฅ 
  • `` ์•ˆ์— ๋‹ค์–‘ํ•œ ๋ณ€์ˆ˜ ๋ถˆ๋Ÿฌ ์˜ฌ ์ˆ˜ ์žˆ์Œ (array, object ..)
  • ๋‹จ, `` ์•ˆ์— ๋“ค์–ด๊ฐ€๋ฉด ์ „๋ถ€ string ์ฒ˜๋ฆฌ ๋จ

 

๐Ÿ“Œ ์—ฐ์‚ฐ์ž 

โœ… ๊ธฐ๋ณธ ์—ฐ์‚ฐ์ž

// ์ œ๊ณฑ๊ทผ
let a = 8 ** 2;
console.log(a);
let num = Math.sqrt(64);
console.log(num);
let result1, result2;
let num = 10, num2 = 10;

result = num++;
console.log(result); // result = 10

result2 = ++num2;
console.log(result2); // result = 11

result ๊ฐ™์€ ๊ฒฝ์šฐ num์— ++ํ•˜๊ธฐ ์ „์— result์— num ๋Œ€์ž…

result2๋Š” ++ํ•œ num์„ result2์— ๋Œ€์ž…

 

โœ… ๋น„๊ต ์—ฐ์‚ฐ์ž

let a = 1;
let b = "1";
// ๋น„๊ต ์—ฐ์‚ฐ์ž
console.log(a == b); //true
// ์ผ์น˜ ์—ฐ์‚ฐ์ž
console.log(a === b); //false

๋น„๊ต ์—ฐ์‚ฐ์ž(==)๋Š” JS ํŠน์„ฑ ๋•Œ๋ฌธ์— ์ž๋™ ํ˜•๋ณ€ํ™˜์ด ์ผ์–ด๋‚˜์„œ true๊ฐ’์ด ๋‚˜์˜ค๋Š” ๊ฒƒ 

์ผ์น˜ ์—ฐ์‚ฐ์ž(===)๋Š” ๋ณ€์ˆ˜์˜ ๊ฐ’ ๋ฟ๋งŒ ์•„๋‹ˆ๋ผ ์ž๋ฃŒํ˜• ๊นŒ์ง€๋„ ๋น„๊ต

=== ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์ถ”์ฒœ

 

โœ… ๋…ผ๋ฆฌ ์—ฐ์‚ฐ์ž

// ์—ฌ์„ฑ์ด๊ณ , ์ด๋ฆ„์ด Jane ์ด๊ฑฐ๋‚˜ ์„ฑ์ธ์ด๋ฉด ํ†ต๊ณผ
let gender = "M";
let myName = "Tom";
let isAdult = true;
if (gender === "F" && (myName === "Jane" || isAdult === true)) {
  console.log("ํ†ต๊ณผ");
} else {
  console.log("๋Œ์•„๊ฐ€");
}

๊ฒฐ๊ณผ : ๋Œ์•„๊ฐ€ ๋ฐ˜ํ™˜ 

์—ฐ์‚ฐ์„ ์ž…๋ ฅ ์ˆœ์„œ๋Œ€๋กœ ๋น„๊ตํ•˜๊ธฐ ๋•Œ๋ฌธ์— ๊ด„ํ˜ธ ์ž˜ ์จ์ฃผ๊ฑฐ๋‚˜ ์ˆœ์„œ ์ฃผ์˜ํ•˜๊ธฐ 

 

๐Ÿ“Œ ๋ฐ˜๋ณต๋ฌธ

โœ… do.. while ๋ฌธ

์ฝ”๋“œ๊ฐ€ ์ตœ์†Œ 1๋ฒˆ์€ ์‹คํ–‰๋จ

// do - while ๋ฌธ
let i = 1;
do {
  console.log(`do-while ๋ฌธ์—์„œ ${i} ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค!`);
} while (i < 0);

// while ๋ฌธ
i = 1;
while (i < 0) {
  console.log(`while ๋ฌธ์—์„œ ${i} ๋ฒˆ์งธ ๋ฐ˜๋ณต๋ฌธ์„ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค!`);
}

 

๐Ÿ“Œ ํ•จ์ˆ˜

โœ… ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ

ํ•จ์ˆ˜๋“ค ๋ฏธ๋ฆฌ ๋‹ค ์ฝ์–ด์„œ ์–ธ์ œ ์–ด๋””์„œ๋“  ํ˜ธ์ถœํ•˜์—ฌ ์“ธ ์ˆ˜ ์žˆ์Œ

function sayHello(){
  console.log('Hello');
}

 

โœ… ํ•จ์ˆ˜ ํ‘œํ˜„์‹

ํ•จ์ˆ˜ ํ‘œํ˜„์‹์ด ๋จผ์ € ๋‚˜์™€์•ผ ํ˜ธ์ถœ์ด ๊ฐ€๋Šฅ

let sayHello = function(){
  console.log('Hello');
}

 

โœ… ํ™”์‚ดํ‘œ ํ•จ์ˆ˜(arrow function)

let add =function(num1,num2){
  return num1+num2;
}
// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜๋กœ ๋ณ€๊ฒฝ
let add = (num1,num2)=> {return num1+num2};

 

โœ”๏ธ ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ vs ํ•จ์ˆ˜ ํ‘œํ˜„์‹ vs ํ™”์‚ดํ‘œ ํ•จ์ˆ˜ 

// ํ•จ์ˆ˜ ์„ ์–ธ๋ฌธ
function sayHello() {
  console.log("Hello");
}
// ํ•จ์ˆ˜ ํ‘œํ˜„์‹
let sayHello = function () {
  console.log("Hello");
};
// ํ™”์‚ดํ‘œ ํ•จ์ˆ˜
let sayHello = () => {console.log("Hello")}

 

๐Ÿ“Œ ๋ฐฐ์—ด 

: ์ˆœ์„œ๊ฐ€ ์žˆ๋Š” list

  • length: ๋ฐฐ์—ด์˜ ๊ธธ์ด 
  • push(): ๋ฐฐ์—ด ๋์— ์ถ”๊ฐ€ 
  • pop(): ๋ฐฐ์—ด ๋ ์š”์†Œ ์ œ๊ฑฐ
  • shift(): ๋ฐฐ์—ด ์•ž ์š”์†Œ ์ œ๊ฑฐ 
  • unshift(): ๋ฐฐ์—ด ์•ž ์š”์†Œ ์ถ”๊ฐ€

 

๐Ÿ“Œ ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹

let hello = "Hello";
// .split : ๋ฌธ์ž๋ฅผ ์ธ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ์ชผ๊ฐœ์„œ ๋ฐฐ์—ด๋กœ ๋ฐ˜ํ™˜
let helloArr = hello.split("");
console.log(helloArr);

// .reverse : ๋ฐฐ์—ด์˜ ์ˆœ์„œ๋ฅผ ๋’ค์ง‘์–ด์„œ ๋ฐ˜ํ™˜
let reverseArr = helloArr.reverse();
console.log(reverseArr);

// .join : ๋ฐฐ์—ด์„ ์ธ์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ๋ณ‘ํ•ฉํ•ด์„œ ๋ฐ˜ํ™˜ (<-> .split)
let joinStr = reverseArr.join("");
console.log(joinStr);

 

โœ… ๋ฉ”์†Œ๋“œ ์ฒด์ด๋‹

์—ฌ๋Ÿฌ ํ•จ์ˆ˜(method)๋ฅผ ์—ฐ๊ฒฐํ•˜์—ฌ ์‚ฌ์šฉ 

let hello = "Hello";
let result = hello.split("").reverse().join("");
console.log(result);

์ฃผ์˜, return ๊ฐ’์ด ์žˆ์–ด์•ผ๋งŒ ๊ฐ€๋Šฅ 

 

728x90