2021년 2월 5일 금요일

[Javascript/TypeScript] 변수의 타입 가져오기.

1
2
3
const a = [];
console.log(typeof a);
console.log(a.constructor.name);
cs

 배열 생성 후 typeof로 찍어보면 object가 나온다.

정확하게 찍으려면 변수의 contructor의 name을 가져오면 된다.

타입을 비교하려면 

1
2
const a = [];
console.log(a.constructor === Array);
cs

변수의 constructor와 Array같은 타입의 Constructor(예를 들면 ArrayConstructor)와 비교하면 된다.

댓글 없음: