Something Like that:
Existence and Boolean
What is Existence
Goes to internet and looks for a value.
For that you need to create first html code.
HTML
<p>Value of a = <span id="demo"></span></p>
After that add this JS code.
JS
var a;
a = '';
if(a){
document.getElementById("demo").innerHTML = 'something is there!';
} else {
document.getElementById("demo").innerHTML = 'Nothing is there!';
}
You can see a = ”; So now It will be print ‘Nothing is there!’
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”WEoOyX” default_tab=”result” user=”pradeepanvi”]See the Pen Existence and Boolean in javascript by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Value of a = Nothing is there!
Other side I want this should be print ‘something is there!’
JS
var a;
a = 'something';
if(a){
document.getElementById("demo").innerHTML = 'something is there!';
} else {
document.getElementById("demo").innerHTML = 'Nothing is there!';
}
You can see a = ‘something’; So now It will be print ‘something is there!’
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”GvNEXN” default_tab=”result” user=”pradeepanvi”]See the Pen Existence and Boolean in javascript 02 by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Value of a = something is there!
Let’s try Boolean as well true/false.
JS
var a;
a = false;
if(a){
document.getElementById("demo").innerHTML = 'something is there!';
} else {
document.getElementById("demo").innerHTML = 'Nothing is there!';
}
You can see a = false; So now It will be print ‘Nothing is there!’
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”RZogqG” default_tab=”result” user=”pradeepanvi”]See the Pen Existence and Boolean in javascript 03 by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Now you can see
Value of a = Nothing is there!
JS
var a;
a = true;
if(a){
document.getElementById("demo").innerHTML = 'something is there!';
} else {
document.getElementById("demo").innerHTML = 'Nothing is there!';
}
You can see a = ‘something’; So now It will be print ‘something is there!’
[codepen_embed height=”265″ theme_id=”0″ slug_hash=”ZJBywV” default_tab=”result” user=”pradeepanvi”]See the Pen Existence and Boolean in javascript 04 by Pradeep Kumar (@pradeepanvi) on CodePen.[/codepen_embed]
Value of a = something is there!
You can try number as well.