javascript - Check if variable is not undefined because it's === true or has a value assigned -


Sometimes I see that it is normal to do this on javascript For whether the variable is not undefined because it is assigned a value :

  if (variable) ) {/ * Not undefined However, I was asking myself how can I check that if  variable  is not  undefined  is not in the following /   

Status:

Live demo:

  $ (f Urection () {var person = create ('John'); var isMarried = person hemorrhoid; console.log ('smile is:' + smile is); if (has been) {console.log ('do something!' )}}}); Create function (name) {var person = {}; person. Name = name; Person.isMarried = getRandom (); Return person; }; Function getRandom () {var arr = [true, 'no', undefined]; Var Rand = ARR [Math. Floor (Math.Random * * arr.length); Return Rand;};   

In this case Harried Variables < Code> true , 'no' , or undefined .

So, my questions are ...

1) How can I check if harried variable no undefined because it is === true (equals true) or because its value was assigned is? (This is the last one when happens === 'no' ).

2) Is it possible to check it without using an additional if statement or condition?

3) What is the better way to test it?


In both the situations described above, (at number 1) is found in the if statement in the browser console Check Output from:

  has been hacked: Do something true! Surprised: Undefined is Elite: Do not Do Something!    

PS I'm just using jQuery for testing, this question is not related to that framework! Thank you.

You have three options for a single parallelism test:

  1. Critical equality if (person.isMarried === true) or if (person.isMarried! == undefined) . Check whether any variable is clearly something (without any kind of conversion allowed).

  2. Loose similarity if type (person.isMarried == true) type conversions are allowed.

  3. Any true / incorrect value if (person.ismaried) . If there is a true value in the person.nosigned , then it will be deemed to be. Even "no" will be a true value.

    If you are trying to tell the difference between "no" , false and Undefined , you must use more than one comparison because these are different values ​​of different types.


    If you just want to know that there is a value in the variable (such as is not undefined), then you can use strict similarity probes and the actual undefined value You can compare:

      if (person.isMarried! == undefined) {// There is some value in person. Although merged, it can be anything other than an uncertain value}    

Comments

Popular posts from this blog

c - Mpirun hangs when mpi send and recieve is put in a loop -

python - Apply coupon to a customer's subscription based on non-stripe related actions on the site -

java - Unable to get JDBC connection in Spring application to MySQL -