javascript
javascript의 null과 undefined
우혁이 아빠
2010. 11. 9. 11:59
null도 객체
undefined 가 null로 인식된단다.
예)
undefined 가 null로 인식된단다.
예)
1. 선언된 변수.
2. 미선언 변수.
미선언 변수는 참조나 비교시 에러가 발생하나 특별히 typeof 연산자에서는 undefined를 반환한다.
3. 함수의 리턴
4. null와 undefined의 차이
null == undefined
출처 --> http://www.eguru.co.kr/324
var temp;
alert(temp == null); // true
alert(temp == undefined); // true
alert(typeof temp); // 'undefined'
2. 미선언 변수.
미선언 변수는 참조나 비교시 에러가 발생하나 특별히 typeof 연산자에서는 undefined를 반환한다.
.
// temp2변수를 선언하지 않았을 때.
alert(typeof temp2); // 'undefined'
alert(temp2); // error.
alert(temp2 == undefined); // error.
3. 함수의 리턴
function tempFn(name) {
// empty function.
};
alert(tempFn()); // 'undefined'
4. null와 undefined의 차이
null == undefined
엄밀히 말해 null은 참조하는 객체가 없음(null)임을 나타내고, undefined는 그 변수가 참조하는 객체를 아직 지정하지 않았음(not initialized)을 뜻한다. 따라서 undefind변수는 할당을 통해 값을 가지며 이 값(객체)를 해제할때는 변수에 null을 할당한다.
alert(null == undefined); // 'true'
var x; // x = undefined
x = {name:'kim', age:30}; // x = Object
x = null; // x = null (이때 Object가 해제된다)
출처 --> http://www.eguru.co.kr/324