Activity Forums Salesforce® Discussions Difference between 'null' and 'undefined' In Apex

  • Avnish Yadav

    Member
    September 12, 2018 at 1:26 pm

    Hello,

    If you declare a variable and don't initialize it with a value, it will be null. In essence, null means the absence of a value while if you use variable and not declare it data types then it is undefined.

    Thanks.

  • shariq

    Member
    September 12, 2018 at 1:39 pm

    Hi,

    There is nothing like undefined in apex. we have undefined in case of JavaScript. Instead of asking Difference between 'null' and 'undefined' In Apex, you can ask what is difference between 'null' and 'undefined'.

    JavaScript assigns 'undefined' to any object that has been declared but not initialized or defined.

    Apex assigns 'null' to any object that has been declared but not initialized or defined

    Thanks

     

  • Parul

    Member
    September 12, 2018 at 1:39 pm

    Hi

    Undefined means a variable has been declared but has not yet been assigned a value, such as:

    var TestVar;
    alert(TestVar); //shows undefined
    alert(typeof TestVar); //shows undefined

    null is an assignment value. It can be assigned to a variable as a representation of no value:

    var TestVar = null;
    alert(TestVar); //shows null
    alert(typeof TestVar); //shows object

     

    THanks

  • chanchal kumar

    Member
    September 12, 2018 at 2:32 pm

    Hi,

    There are two special clauses in the "abstract equality comparison algorithm" in the JavaScript spec devoted to the case of one operand being null and the other being undefined, and the result is true for == and false for !=. Thus if the value of the variable is undefined, it's not != null, and if it's not null, it's obviously not != null.

Log In to reply.

Popular Salesforce Blogs