var 변수에 아무것도 대입하지 않고 선언만 하면 이 변수는 undefined 가 된다(정의 자체가 되지 않았다는 뜻)
그렇지만 null 을 대입하게 되면 이변수는 null 값을 담게 된다(아무것도 참조 하지 않는다 라는 뜻)
그런데 undefined 이든 null 을 갖고 있는 변수든 다음 행에 정수 값을 대입하면 이 변수의 타입은 number 가 된다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | // A variable that has not been initialized. var ss ; console.log("typeof ss is " + typeof (ss)); var ss1 = null; console.log("typeof ss1 is " + typeof (ss1)); var declared; if (declared == undefined) console.log("declared 는 값이 주어지지 않았다"); else console.log("declared 는 값이 주어졌다"); console.log("typeof declared is " + typeof (declared)); // An undeclared variable cannot be compared to undefined, // so the next line would generate an error. // if (notDeclared == undefined); console.log("typeof notDeclared is " + typeof (notDeclared)); // Output: // declared has not been given a value // typeof declared is undefined // typeof notDeclared is undefined | cs |
결과 화면
undefined 상수(JavaScript)
초기화되지 않은 변수와 같이 정의되지 않은 값입니다.
undefined 상수는 Global 개체의 멤버이고 스크립팅 엔진이 초기화되면 사용할 수 있습니다.변수가 초기화되지 않고 선언되면 그 값은 undefined입니다.
변수가 선언되지 않으면 undefined와 비교할 수 없지만 변수 유형은 문자열 "undefined"와 비교할 수 있습니다.
undefined 상수는 변수를 명시적으로 테스트하거나 undefined로 설정할 경우 유용합니다.
다음 예제에서는 undefined 상수를 사용하는 방법을 보여 줍니다.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | // A variable that has not been initialized. var declared; if (declared == undefined) document.write("declared has not been given a value <br/>"); else document.write("declared has been given a value <br/>"); document.write("typeof declared is " + typeof(declared) + "<br/>"); // An undeclared variable cannot be compared to undefined, // so the next line would generate an error. // if (notDeclared == undefined); document.write("typeof notDeclared is " + typeof(notDeclared)); // Output: // declared has not been given a value // typeof declared is undefined // typeof notDeclared is undefined |
요구 사항
undefined 속성은 Internet Explorer(Internet Explorer 6보다 오래된 버전)에 도입되었고 Internet Explorer 9 표준 모드에서는 읽기 전용으로 만들어졌습니다.
null은 변수가 유효한 데이터를 참조하지 않음을 나타내는 데 사용됩니다. undefined 상수(JavaScript)과는 다릅니다.
ref : https://msdn.microsoft.com/ko-kr/library/fhcc96d6(v=vs.94).aspx
ref : https://msdn.microsoft.com/ko-kr/library/dae3sbk5(v=vs.94).aspx
반응형
'서버(Server) > Server' 카테고리의 다른 글
javascript : 함수가 아닌 곳에서의 변수는 전역변수로 생성된다 for, if 문 안의 변수 또한 ... (0) | 2018.04.20 |
---|---|
javascript : 변수 타입과 typeof , 6가지 타입 (0) | 2018.04.20 |
javascript : 함수와, 즉시실행함수로 전역변수 사용하지 않기 (0) | 2018.04.20 |
javascript : 배열, 객체와 반복문처리(key & value) for, foreach, "0" (0) | 2018.04.19 |
javascript : 객체와 배열 구분과 사용 (0) | 2018.04.18 |