๐Ÿ—’ Javascript ๋ฐ์ดํ„ฐํƒ€์ž…

Dayยท2022๋…„ 4์›” 4์ผ
0
post-thumbnail

๐Ÿ”ฃ ๋ฐ์ดํ„ฐ ํƒ€์ž…

string
number
boolean

โœ” Boolean

โœ” null & undefined

undefinedย ย ย ย ์„ ์–ธ์€ ๋์ง€๋งŒ ์•„์ง value๊ฐ€ ํ• ๋‹น๋˜์ง€ ์•Š์€ ๊ฒฝ์šฐ
null ย ย ย ย ย ย ย ย ย ย ย ย '๋นˆ ๊ฐ’(blank)'์„ ์˜๋ฏธํ•˜๋Š”๋ฐ ์‚ฌ์šฉ์ž๊ฐ€ ์ค€ value

typeof

()์—†์ด ๊ทธ๋ƒฅ ์“ด๋‹ค. ๋ฐ์ดํ„ฐ ํƒ€์ž… ์•Œ ์ˆ˜ ์žˆ์Œ

let msg = "message";

console.log(typeof msg); // "string"
console.log(typeof 100); // "number"

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

๋™๋“ฑ ์—ฐ์‚ฐ์ž (==)
๋‘ ํ”ผ์—ฐ์‚ฐ์ž์˜ ์ž๋ฃŒํ˜•์„ ์ผ์น˜์‹œํ‚จ ํ›„, ์—„๊ฒฉํ•˜๊ฒŒ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰

๋ถ€๋“ฑ ์—ฐ์‚ฐ์ž (!=)
๋‘ ํ”ผ์—ฐ์‚ฐ์ž์˜ ์ž๋ฃŒํ˜•์„ ์ผ์น˜์‹œํ‚จ ํ›„, ์—„๊ฒฉํ•˜๊ฒŒ ๋น„๊ต๋ฅผ ์ˆ˜ํ–‰

์ผ์น˜ ์—ฐ์‚ฐ์ž (===)
์ž๋ฃŒํ˜• ๋ณ€ํ™˜ ์—†์ด ๋‘ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ์—„๊ฒฉํžˆ ๊ฐ™์€์ง€ ํŒ๋ณ„

๋ถˆ์ผ์น˜ ์—ฐ์‚ฐ์ž (!==)
์ž๋ฃŒํ˜• ๋ณ€ํ™˜ ์—†์ด ๋‘ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ์—„๊ฒฉํžˆ ๊ฐ™์€์ง€ ํŒ๋ณ„

๋‘ ํ”ผ์—ฐ์‚ฐ์ž๊ฐ€ ๊ฐ™์ง€ ์•Š๊ฑฐ๋‚˜, ๊ฐ™์€ ์ž๋ฃŒํ˜•์ด ์•„๋‹ ๋•Œ true๋ฅผ ๋ฐ˜ํ™˜.
์ •๋ฆฌํ•˜์ž๋ฉด ๋™๋“ฑ ๋น„๊ต ์—ฐ์‚ฐ์ž(==, !=)์™€ ์ผ์น˜ ๋น„๊ต ์—ฐ์‚ฐ์ž(===, !==)์˜ ๊ฐ€์žฅ ํฐ ์ฐจ์ด์ ์€ ํ˜•๋ณ€ํ™˜์„ ํ•˜๋ƒ ์•ˆํ•˜๋ƒ์ด๋‹ค.

๐Ÿ“… ๋‚ ์งœ, ์‹œ๊ฐ„

๋‚ ์งœ ๊ฐ์ฒด๋ฅผ ํ˜ธ์ถœํ•˜๋ฉด ์‰ฝ๊ฒŒ ์‹œ๊ฐ„, ๋‚ ์งœ๋ฅผ ์–ป์„ ์ˆ˜ ์žˆ๋‹ค.
new์—ฐ์‚ฐ์ž ๋‹ค์Œ์— Date์ƒ์„ฑ์ž๋ฅผ ์“ด๋‹ค.

new Date() ์ด๋ ‡๊ฒŒ ์“ฐ๋ฉด ํ˜„์žฌ ์‹œ๊ฐ„ ๋‚ ์งœ๊ฐ€ ํ˜ธ์ถœ ๋˜๋Š” ๊ฑฐ ๊ฐ™๊ณ 

let date1 = new Date('December 17, 2019 03:24:00');
let date2 = new Date('2019-12-17T03:24:00');
let date3 = new Date(2019, 5, 1);

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ํŠน์ • ๋‚ ์งœ์˜ Date๊ฐ€ ๋ฐ˜ํ™˜๋œ๋‹ค.

var rightNow = new Date();

Date ๊ฐ์ฒด๋กœ ํ˜„์žฌ ์‹œ๊ฐ„์„ ๊ฐ€์ ธ์˜จ ํ›„, Date ๊ฐ์ฒด๊ฐ€ ๊ฐ–๊ณ  ์žˆ๋Š” ํ•จ์ˆ˜๋กœ
๊ฐ ๋‚ ์งœ, ์‹œ๊ฐ„์˜ ๊ฐ’์„ ๊ฐ€์ ธ์˜ฌ ์ˆ˜ ์žˆ๋‹ค. 

let rightNow = new Date();
let year = rightNow.getFullYear();
let month = rightNow.getMonth()+1;
let date = rightNow.getDate();
let day = rightNow.getDay();
let currentHour = rightNow.getHours();
let currentMin = rightNow.getMinutes();

getMonth() ๋ฉ”์„œ๋“œ๋Š”, ํ˜„์žฌ ๋‹ฌ๋ณด๋‹ค 1 ์ž‘์€ ๊ฐ’์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๋˜, ์ •ํ™•ํ•œ ์‹œ๊ฐ„์€, ์œ„์˜ ์ฝ”๋“œ 1๋ฒˆ์งธ ์ค„์ด ์‹คํ–‰๋˜๋Š” ์ˆœ๊ฐ„์˜ ํ˜„์žฌ ์‹œ๊ฐ„์ด ๋‹ด๊ธฐ๋Š” ๊ฒƒ.

getTime() ๋ฉ”์†Œ๋“œ๋Š” ๋ฐ€๋ฆฌ์ดˆ ํ‘œํ˜„์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

let rightNow = new Date();
let time = rightNow.getTime();

์ƒˆ๋กœ new Date() ํ˜ธ์ถœํ•˜๋ฉด ์‹œ๊ฐ„์ด ์ง€๋‚ฌ์–ด์„œ ๋” ํฐ ์ˆซ์ž๊ฐ€ ๋‚˜์˜ด.

์ด๋ ‡๊ฒŒ ํ•ด์„œ getTime ๋ฉ”์†Œ๋“œ์— ๋ฐ˜ํ™˜๋œ ์ˆซ์ž๋กœ ๋น„๊ต์—ฐ์‚ฐ์„ ํ†ตํ•ด ์–ธ์ œ๊ฐ€ ๋” ๊ณผ๊ฑฐ์ธ์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.

function getWesternAge(birthday) {
let birth = new Date(birthday);
 let now = new Date("2020-7-21");

  let year = now.getFullYear() - birth.getFullYear() ;

  let nowMonth = now.getMonth() + 1
  let birthMonth = birth.getMonth()+1

  let m = birthMonth - nowMonth;

  if( m < 0 || m === 0 && (birth.getDate() < now.getDate())){


    return year 
  }
  else{


return year-1 
 
}
}
console.log(getWesternAge("1990-03-21")); // 30 

๋งŒ ๋‚˜์ด๋ผ๋Š” ๊ฒŒ ์ด๋Ÿฐ ๊ฑด์ง€ ์ž˜ ๋ชจ๋ฅด๊ฒ ์ง€๋งŒ, ์•„๋ฌดํŠผ !

ํ˜„์žฌ๋ž‘ ๋น„๊ตํ•ด์„œ ์ƒ์ผ์ด ์ง€๋‚ฌ์œผ๋ฉด( ํ˜„์žฌ๊ฐ€ ๋” ํฐ ์ˆ˜๋ฉด) ๊ทธ๋Œ€๋กœ year

์•„๋‹ˆ๋ฉด year -1

0๊ฐœ์˜ ๋Œ“๊ธ€