0

Ok i have little section of javascript syntax and i am very confuse how null behaves. There is a lot of discussion about null values but i cant seem to figure out the problem! Please help me. Here is the script.

var jsonData = '<?php echo $jsonData;?>';


    if (jsonData)
    {           
        console.log('jsonData is '+ jsonData);// null or not this section is always executed! why?
    }else{
        ini(jsonData);
    }

I tried using '===', '!' operators but still not working as expected

4
  • <?php ?> tags are wrapped in quote..returned value will always be string like 'null' which is truthy value.. Commented Mar 1, 2016 at 5:04
  • I see so its a string lol. Well console.log didn't show any quotation though. Commented Mar 1, 2016 at 5:07
  • Console will never show quotes..console.log('hi'); => hi not "hi".. Commented Mar 1, 2016 at 5:08
  • i see thank your for that one. ill be sure to remember it Commented Mar 1, 2016 at 5:11

2 Answers 2

1

this line var jsonData = '<?php echo $jsonData;?>'; will always be a string, and if the value returned from php is null, then you will end up having var jsonData = 'null';

try this code

if (jsonData && jsonData != 'null' )
{           
    console.log('jsonData is '+ jsonData); //now this code will be executed only if jsonData is not null
}
else
{
    ini(jsonData);
}

or as mentioned by Rayon above, replace the assignment statement with

var jsonData = <?php echo $jsonData;?>;

Sign up to request clarification or add additional context in comments.

Comments

0
Make a check for "Null" value as well as "NAN" in your condition.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.