SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict ketting00's Avatar
    Join Date
    Jul 2011
    Posts
    310
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Why you set variable to null at the beginning of JavaScript

    I notice that several JavaScript experts begin their code with something like this:
    Code:
    var x = null;
    
    function y() {
    	// do something with x
    }
    What is the benefit of this method. I asked Google and it can't help me understand the concept.

    Thank you,

  2. #2
    SitePoint Addict
    Join Date
    Apr 2011
    Posts
    266
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Hi
    I think that the reason is to make sure that the variable is defied, and it is in global scobe (available in all functions).
    Free: Web Programming Courses HTML, CSS, Flash
    Web Programming: AJAX Course and PHP-MySQL Course video Lessons
    Good JavaScript and jQuery course for beginners

  3. #3
    SitePoint Wizard bronze trophy chris.upjohn's Avatar
    Join Date
    Apr 2010
    Location
    Melbourne, AU
    Posts
    2,183
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    All variables by default have a null value which is equal to undefined, false, 0 and of course null, to ensure a variable has a true value meaning something meaningful you could simply use the following.

    Code JavaScript:
    var x;
     
    // Returns "value not defined"
    alert('value ' + (x ? 'not' : 'is') + ' defined');
     
    x = 'test';
     
    // Returns "value is defined"
    alert('value ' + (x ? 'not' : 'is') + ' defined');
    Blog/Portfolio | Evolution Xtreme | DFG Design | DFG Hosting | CSS-Tricks | Stack Overflow | Paul Irish
    Having lame problems with your code? Let us help by using a jsFiddle

  4. #4
    Non-Member
    Join Date
    Feb 2012
    Posts
    892
    Mentioned
    10 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chris.upjohn View Post
    All variables by default have a null value which is equal to undefined, false, 0
    You mean a falsy value. null value is null value, it's not "equal to undefined, false, 0", this only adds to the confusion.
    EDIT: the default value in JS is undefined.

    Quote Originally Posted by chris.upjohn View Post
    and of course null, to ensure a variable has a true value meaning something meaningful you could simply use the following.
    Again, a falsy value, a true value it's not.


    The discussion here is about three things: undeclared, undefined or null.

    Using x without declaring it first (without using an explicit var statement) denotes a variable which declaration will be hoisted by the interpreter. This also usually leads to unexpected behavior, where shadowing a variable leads to a completely different result. The recommendation is to always declare your variables at the top of your scope.

    Declaring x outside any function denotes a global variable (usually not a pattern), where, if no value is assigned, a undefined value is the default value. So using var x = null overrides the default undefined value.

    Explicitly using var x = null in comparison with undefined, ensures that, when using "===" instead of "==", you'll get the right false value, false.

    Code:
    >console.log(null == undefined);
    true
    
    > console.log(null === undefined);
    false


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •