Reputation: 15
code have to do 3 things
1st problem i get TypeError data.sream is null even if i define null as in examples. some people say`ed that i can just ignore that, but i want to ask professionals -- Pritam Banerjee answer this question thank you
2nd problem is that user is not pushed into array. only same user is pushed few times in array. Code is here Fiddle
sorry if question is not good but i dont know where else to ask.
var mynull = null;
if (typeof mynull === null) { // have to be if(mynull == null)
// my code
}
else
//my code
// building prototype name & status
function userStatus(name, status) {
this.name = name;
this.status = status;
} // prototype
var showUserList = ["OgamingSC2", "habathcx", "RobotCaleb", "noobs2ninjas"]
var lastData = []; // where to push users
for (var i = 0; i < showUserList.length; i++) {
var user = showUserList[i]; // get username from array
var useris = showUserList[i] // --//--
useris = new userStatus(user, 'Chanel is Offline');
lastData.push(useris);
console.log(lastData[i].name + '' + lastData[i].status);
}
Upvotes: 1
Views: 970
Reputation: 18923
This will not work as:
typeof null === 'object';
So it will always return an object and hence your code will not work.
Rather you can try:
if (myNull == null){
// your code here.
}
Also can try as suggested by Scott, but the if else would be the other way round:
if (myNull){}
Upvotes: 0
Reputation: 65808
null
is an important primitive type in JavaScript. It is essentially a special value that indicates that there is no value! Initializing your variables to null
when you don't know yet what their actual value will be later on is a very good best-practice in JavaScript, because if you wind up getting an error (like the one you are getting now (TypeError data.sream is null), it tells you that you never actually supplied a useful value to the variable.
When it comes to testing for null
, you have a few options, but something that is important to remember is that JavaScript is loosely typed and values can and do change their type simply based on the way you use them. null
is what's known as a "falsey" value, that is, if you convert it to a Boolean, it converts to false. Other "Falsey" values include: 0
, undefined
, ""
and false
. Just about every non-empty, non-zero, non-undefined value is "truthy" and will convert to true
if forced into a Boolean.
So, if you really just want to know if your variable is still null
or does it have meaningful data, you can leverage this loose typing like the following because the value of myNull
will be converted to a Boolean so that the if
statement can be evaluated.
Ask yourself if you really care if your variable has the exact value of null
or do you just need to know if your variable has data that you can use? An explicit test for null may not really even be needed.
var myNull = null;
var myNull2 = null;
// We're only going to give one of the variables a value
myNull2 = "something";
if(myNull){
// myNull is NOT null
console.log("myNull is NOT null and has a value of: " + myNull);
} else {
// myNull is null
console.log("myNull IS null and has a value of: " + myNull);
}
if(myNull2){
// myNull2 is NOT null
console.log("myNull2 is NOT null and has a value of: " + myNull2);
} else {
// myNul2l is null
console.log("myNul2l IS null and has a value of: " + myNull2);
}
Upvotes: 1
Reputation: 287980
Some things you must know about the typeof
operator:
It always returns a string. Never compare the returned value to a non-string using ===
.
It's horribly broken because it doesn't always not return the Type of the value.
For example, typeof null
returns the string 'object'
.
The proper way to compare mynull
with null
is mynull === null
.
You can also use mynull == null
to detect if it's either null
or undefined
.
Upvotes: 1