Search

1/22/2008

null & undefined

The undefined value is returned in three cases:
* If you declared a variable but did not assign a value to it.
* If you access an undefined property of an object (and in JavaScript you can see anything you like as a property of an object).
* If you have defined a function argument, but no value is passed to it. (See 5I for arguments.)


0 || 100 // 0
1 || 100 // 1


document.getElementById('nosuchid') // return null
typeof (document.getElementById('nosuchid')) // object, 'cause typeof null == 'obejct'
null||'test' // test

firebug中如果沒有印出任何值,那就是'undefined'
access an undefined property of an object

var foo = {'bar':'bar'};
foo.tttt // nothing happen, it's 'undefined'
foo.bar||'else' // 'bar'

window.ttttt // nothing happen, it's 'undefined'
window.tttt||'else' // 'else'
typeof window.tttt // 'undefined'
typeof window.tttt == 'undefined' //true
typeof window.tttt === 'undefined' //true

declare a variable but not assign value to it:

var tttt;
tttt // nothing happen, it's 'undefined'
alert(tttt) // undefined
typeof tttt // 'undefined'
alert(typeof tttt) // undefined

not declare at all

tttt // error: tttt is not defined
tttt == 'undefined' // error: tttt is not defined
tttt||'test' // error: tttt is not defined
typeof tttt // 'undefined'
typeof tttt == 'undefined' //true
typeof tttt === 'undefined' //true

沒有留言: