javascript undefined not null, not empty issue - javascript

Is the statement:
if(attachmentId!=null && attachmentId.length>0&& attachmentId !='undefined'){
//do something
}
equivalent to:
if (attchmentId) {
//do something
}
thanks for the help

Writing :
if (attchmentId)
is the equivalent of:
if(
attchmentId != undefined &&//NO QUOTE
attchmentId != '' &&
attchmentId != null &&
attchmentId != false &&
attchmentId != 0
)

They're not equivalent but the third test attachmentId !='undefined' was probably an error (did you want attachmentId !=undefined ?).
Another example of difference is that 0 doesn't pass the first test but pass the second one.
You must decide what's important to you before you try to write the test. If you know you start with a string and you want to test if it's defined and not empty, then you may use the second test.

It can be reduced to this:
if (attachmentId && attachmentId.length > 0) {
//do something
}
This will do for arrays and more complex objects that happen to have a length property. If attachmentId is supposed to be a string the code above will work the same, but the second part will basically be a noop, so you can just go with:
if (attachmentId) {
//do something
}
I am assuming the comparison against 'undefined' was a mistake - do that and you're not checking if something is actually undefined. You're checking it it is different from a literal string that says "undefined".
Also you check the variable first due to the short circuit rule. It it's either null or not defined you don't care about the rest. Otherwise, if you tried evaluating the length before checking if it's undefined or null you could throw an error there.

Related

Check of object length in Javascript

I want to check if my object is null or the length is 0. I have tried something like this without any luck.
if (this.get('goodie.pincodes.firstObject') == null || this.get('goodie.pincodes.firstObject.pin').length == 0) {
I solved this in a different way. Making sure that entries with no length allways returns null. Then only asking if the object is null.
this.get('goodie.pincodes.firstObject') == null

Why IF parameters doesn't seems like IF parameters?

Recently, I came over a script where the if parameters are not evaluated. The code and quantity in the following JScript is not judged on and =, > or < equations.
if (CODE && QTY) {
// do something
}
But as per my understanding, it should be something like this:
if (CODE > 100 && QTY < 200) {
// do something
}
What's the solution?
An if statement is executed if the statement within the brackets evaluates is truthy.
In your first example, // do something will be executed as long as CODE and QTY are truthy values.
This means that both CODE and QTY are checked that they are not:
false
0
"" (empty string)
null
undefined
NaN
It's used more of a validity check to make sure that the variables can be worked with, than a check of the specific values.

How to verify if array exist and its length in fewest steps?

I have some if statement, where I want to verify the length of an array. Let's say it is the following:
if (object.someArray.length > 1){
doSomething()
}
However, when someArray is not defined (maybe it is not defined for some object, which is possible), I get an error:
Cannot read property 'length' of null
Which makes sense, as object.someArray is not defined.
My question is: what is the best way to verify that the array exists AND its length, in fewest steps?
Thanks!
The very generic and full test : if array exist and is not empty
if(object && object.someArray && object.someArray.length)
For your case :
if(object.someArray && object.someArray.length > 1)
Edit : For those who don't know this is called duck-typing : we aren't checking really if the object is an array, we're considering that if that object has a property length, it is an array. Lot of library do that. As long as you don't store different types of javascript object (string/array/object) in the same variable, this code is safe.
Try to check array through Array.isArray
if (Array.isArray(object.someArray) && object.someArray.length > 1)
You can do it in one single condition like
if (object.someArray && object.someArray.length > 1){
//Do Something
}
if object.someArray is undefined, it will return false and the next condition will not be evaluated
the best way to verify that the array exists AND its length
The best way is to do exactly as you wrote:
if (array && array.length > 0 { ... }
You can check it this way.
if(object.someArray && object.someArray.length > 1) {
doSomething();
}
If the someArray does not exist then it will be false and it will never check for its length because of the short-circuit.

Weird Javascript If Statement

I'm not sure whats wrong with my if statement. I'm trying to use my model inside my javascript.
if (#Model !== null)
{
if (#Model.Level !== null)
{
//use #Model.Level
}
}
else
{
//use default
}
The Model is null, but it still steps into the first if statement (and breaks on the second one obviously). I've tried #Model, !#Model and != but it still always steps in.
What am I doing wrong? (It's also got squiggly red lines under both !== saying there is a syntax error)
Triple equations work without type castings in JavaScript. In your case you are might get an undefined object which isn't a null value.
For example:
undefined === null //Do not cast when comparing, increased performance.
false
undefined == null //Do cast when comparing, decreased performance.
true
In addition, if #Model value is null then you won't see a null value on client side. It gives you an empty value like this:
if( == null)
{
}
This will cause an error on your javascript side. Null check should be done at server side. For that reason you have to put # value in front of your code to make it server side:
<script>
#if (Model != null) //Server side code.
{
if (Model.Level != null) //still server side code.
{
<text>
alert("your javascript here"); //write javascript on your screen.
</text>
}
}
</script>
In order to check if something is null or undefined in javascript, use if (#model) rather than if ( #model !== null)
http://appendto.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-2/
The reason why it steps into the if statement is because it evaluates to true, no weirdness to be found here. Your browser is not temperamental. For a list comparisons check out this http://dorey.github.io/JavaScript-Equality-Table/
Also note that there is a difference between double and triple =. Triple will type cast
This code looks a LOT like a Razor then Javascript, though you may be trying to mix the two of them together.
Your choices:
1) Convert Model to a JavaScript object using something like this:
Turn C# object into a JSON string in .NET 4
2) Use the Razor if statement and write out your final JavaScript with it.
<script>
// Code assume this is an numeric value
var useThisVariable;
</script>
if (#Model !== null)
{
if (#Model.Level !== null)
{
<script>
useThisVariable = #Model.Level;
</script>
}
}
else
{
<script>
useThisVariable = -1;
</script>
}

JQuery - write conditional when object property is blank

I'm trying to write a conditional for when an object property's value is blank, but it is not triggering. This is my code. Any idea how I should write this?
console.log(vid);
if (vid.video == undefined){
//DO STUFF HERE - Doesn't work
}
The "object" in the screenshot is referenced in the code above as variable vid. I also tried undefined in the conditional.
I see some misunderstading here:
You say when an object property's value is blank but your coded if (vid.video != ""){.
I think you need if (!vid.video) {
use this:
vid.video == undefined
Also what Andrew said. If you want to DO STUFF when the attribute is empty, you should Use "==", and not "!="
if(vid.video != "")
Are you looking to only execute code when video doesn't equal "" but if you want to execute code when it is equal to "" you need
if(vid.video == "")
However if you want to check to see if its undefined you'll need to do
if(vid.video === undefined){
vid.video is not defined here
}
or
if(vid.video){
vid.video is defined here
}

Categories

Resources