Can I set variables to undefined or pass undefined as an argument?

后端 未结 10 1882
北荒
北荒 2020-11-30 15:53

I’m a bit confused about JavaScript’s undefined and null values.

What does if (!testvar) actually do? Does it test for u

10条回答
  •  [愿得一人]
    2020-11-30 16:53

    JavaScript, how to set a variable to undefined on commandline:

    Set a variable to undefined in the js javascript command line terminal that comes with Java on Ubuntu 12.10.

    el@defiant ~ $ js
    
    js> typeof boo
    "undefined"
    
    js> boo
    typein:2: ReferenceError: boo is not defined
    
    js> boo=5
    5
    
    js> typeof boo
    "number"
    
    js> delete(boo)
    true
    
    js> typeof boo
    "undefined"
    
    js> boo
    typein:7: ReferenceError: boo is not defined
    

    If you set a variable to undefined in a javascript:

    Put this in myjs.html:

    
    
        
    
    
    

    It prints this:

    aliens: undefined
    typeof aliens: undefined
    found some aliens: string
    not sayings its aliens but... scramble the nimitz
    aliens deleted
    typeof aliens: undefined
    you sure they are gone? undefined
    

    WARNING! When setting your variable to undefined you are setting your variable to another variable. If some sneaky person runs undefined = 'rm -rf /'; then whenever you set your variable to undefined, you will receive that value.

    You may be wondering how I can output the undefined value aliens at the start and have it still run. It's because of javascript hoisting: http://www.adequatelygood.com/JavaScript-Scoping-and-Hoisting.html

提交回复
热议问题