자바스크립트 유형
JavaScript에는 값을 포함할 수 있는 5가지 다른 데이터 유형이 있습니다.
string
number
boolean
object
function
6가지 유형의 객체가 있습니다.
Object
Date
Array
String
Number
Boolean
그리고 값을 포함할 수 없는 2가지 데이터 유형:
null
undefined
typeof 연산자
연산자를 사용하여 typeof
JavaScript 변수의 데이터 유형을 찾을 수 있습니다.
예시
typeof "John"
// Returns "string"
typeof 3.14
// Returns "number"
typeof NaN
// Returns "number"
typeof false
// Returns "boolean"
typeof [1,2,3,4] // Returns
"object"
typeof {name:'John', age:34}
// Returns "object"
typeof new Date()
// Returns "object"
typeof function () {} // Returns
"function"
typeof myCar
// Returns "undefined" *
typeof null
// Returns "object"
다음 사항을 준수하십시오.
- NaN의 데이터 유형은 숫자입니다.
- 배열의 데이터 유형은 객체입니다.
- 날짜의 데이터 유형은 객체입니다.
- null의 데이터 유형은 객체입니다.
- 정의되지 않은 변수의 데이터 유형이 정의되지 않음 *
- 값이 할당되지 않은 변수의 데이터 유형도 정의되지 않음 *
typeof
JavaScript 개체가 배열(또는 날짜)인지 확인하는 데 사용할 수 없습니다 .
원시 데이터
기본 데이터 값은 추가 속성 및 메서드가 없는 단일 단순 데이터 값입니다.
연산자 는 typeof
다음 기본 유형 중 하나를 반환할 수 있습니다.
string
number
boolean
undefined
예시
typeof "John" // Returns
"string"
typeof 3.14 // Returns
"number"
typeof true // Returns
"boolean"
typeof false // Returns
"boolean"
typeof x
// Returns "undefined" (if x has no value)
복잡한 데이터
연산자는 두 typeof
가지 복합 유형 중 하나를 반환할 수 있습니다.
function
object
연산자 는 typeof
개체, 배열 및 null에 대해 "객체"를 반환합니다.
연산자 는 typeof
함수에 대해 "객체"를 반환하지 않습니다.
예시
typeof {name:'John', age:34} // Returns "object"
typeof [1,2,3,4]
// Returns "object" (not "array", see note below)
typeof null // Returns
"object"
typeof function myFunc(){} // Returns "function"
JavaScript에서 배열은 객체이기 때문에 typeof
연산자는 배열에 대해 " "를 반환 합니다 .object
typeof의 데이터 유형
연산자 는 typeof
변수가 아닙니다. 운영자입니다. 연산자( + - * / )에는 데이터 유형이 없습니다.
그러나 typeof
연산자는 항상 문자열 (피연산자 유형 포함)을 반환합니다.
생성자 속성
속성 은 constructor
모든 JavaScript 변수에 대한 생성자 함수를 반환합니다.
예시
"John".constructor
// Returns function String() {[native code]}
(3.14).constructor
// Returns function Number() {[native code]}
false.constructor // Returns
function Boolean() {[native code]}
[1,2,3,4].constructor
// Returns function Array() {[native code]}
{name:'John',age:34}.constructor
// Returns function Object() {[native code]}
new Date().constructor
// Returns function Date() {[native code]}
function () {}.constructor // Returns
function Function(){[native code]}
생성자 속성을 확인하여 객체가 다음과 같은지 확인할 수 있습니다 Array
("배열"이라는 단어 포함).
예시
function isArray(myArray) {
return myArray.constructor.toString().indexOf("Array") > -1;
}
또는 더 간단하게 객체가 Array 함수 인지 확인할 수 있습니다 .
예시
function isArray(myArray) {
return myArray.constructor
=== Array;
}
생성자 속성을 확인하여 객체가 a
Date
("Date"라는 단어 포함)인지 확인할 수 있습니다.
예시
function isDate(myDate) {
return myDate.constructor.toString().indexOf("Date") > -1;
}
또는 더 간단하게 객체가 Date 함수 인지 확인할 수 있습니다 .
예시
function isDate(myDate) {
return myDate.constructor === Date;
}
찾으시는 주소가 없습니다
JavaScript에서 값이 없는 변수에는 값이 undefined
있습니다. 유형도 undefined
입니다.
예시
let car; // Value is undefined,
type is undefined
값을 로 설정하여 모든 변수를 비울 수 있습니다 undefined
. 유형도 입니다 undefined
.
예시
car = undefined; // Value is undefined,
type is undefined
빈 값
빈 값은 와 관련이 없습니다 undefined
.
빈 문자열에는 유효한 값과 유형이 모두 있습니다.
예시
let car = ""; //
The value is
"", the typeof is "string"
없는
JavaScript null
에서 "아무것도"입니다. 존재하지 않는 존재라고 봐야 합니다.
불행히도 JavaScript에서 의 데이터 유형은 null
객체입니다.
typeof null
객체 인 JavaScript의 버그라고 생각할 수 있습니다 . 그것은 있어야합니다 null
.
다음과 같이 설정하여 개체를 비울 수 있습니다 null
.
예시
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = null; //
Now value is null,
but type is still an object
다음과 같이 설정하여 개체를 비울 수도 있습니다 undefined
.
예시
let person = {firstName:"John", lastName:"Doe", age:50, eyeColor:"blue"};
person = undefined; //
Now both value and type is undefined
정의되지 않은 것과 널의 차이점 - 2020 - 다른 사람
undefined
값 null
은 같지만 유형이 다릅니다.
typeof undefined
// undefined
typeof null
// object
null === undefined
// false
null == undefined
// true