Hi I'm very new to JS.
Lets say I have an object named test which doesn't have a property missing. But I try to access the same:
test.missing //getting undefined as output
Now I'm trying to access the missing properties ( like this test.missing.404 ) which will give me a TypeError. But my book says we can get rid of this TypeError by like this:
test.missing && test.missing.404
Now my big question is how one can do a && against the undefined type and TypeError type. Couldn't able to guess what Js is doing here.
Thanks in advance.
undefined in js is treated as false and if something has value it is treated as true, so:
//if test.missing is NOT undefined and test.missing.404 is also NOT undefined
if (test.missing && test.missing.404)
First of you should know that JS like many other programming languages uses short circuit technique during comparison. So in this case if the first condition is false then there is no need to check the next condition(s). This is due to the fact that 0 && X will always result 0 .
With that said, test.missing && test.missing.404 can be perceive as "if the test object has missing property and the (test object's) missing property has 404 property" then proceed. Which the JS intepreter will completely ignore the check for 404 property on missing property if the test object has no missing property in the first place.
So the point is the above code is not && comparison of undefined and TypeError but rather check for missing property first and 404 property if first property exists. I hope this makes sense ad explains it well enough.
Related
I sometimes have to write code in JavaScript that handles both null and undefined the same way. For example, a function that removes from an array every item that is either null or undefined. How should I name the function?
removeNullAndUndefined is very long. removeNull is short but imprecise. Ideally I want to name it removeX, where X has the meaning "null or undefined".
Does anyone know a good word for X?
Yes: Nullish. For instance, in the current Stage 3 Nullish Coalescing Operator proposal.
So: removeNullish or similar.
When I need to check for null or undefined I often name such function either isNil or isNothing e.g.
const isNil = thing => thing === null || thing === undefined;
If you must adopt a naming convention I'd opt for what's quite common to see in functional programming, namely the Maybe monad which comes with a subtype to represent nothing: Nothing or None.
If you look at monetjs:
The Maybe type is the most common way of representing nothingness (or the null type) with making the possibilities of NullPointer issues disappear.
Maybe is effectively abstract and has two concrete subtypes: Some (also Just) and None (also Nothing).
Under the hood, it uses a isNothing function for checking for null or undefined
In ramda.js, such function is called isNil:
Checks if the input value is null or undefined
(Lodash has a similar method with the exact same name.)
I was wondering if it is safe to assume that in JavaScript a variable will always have a "constructor" property if its value is not "null" or "undefined".
I ran into a situation where I had to check if a variable is defined and that if it is an array and if so check if its length is > 0:
if(variable && variable.constructor === Array && variable.length > 0) {
...loop through the array
}
Am I right to assume that variable will always have a constructor if it is not "undefined" or "null"?
Your if statement won't throw because you've made that assumption, because anything that passes your first check will support your second, even if there is no constructor property (you'll just get undefined), and in fact even if it's not an object (it'll get temporarily promoted). So in that sense yes, you can happily do what you're doing — unless you could be dealing with an array from another window, in which case the === Array will fail (as dandavis pointed out) because different windows have different instances of the Array constructor.
Note that this is not the same as every object having a constructor property. It's just that if the object doesn't, you'll get undefined, rather than an error. (You can get an object with no constructor property by creating an object with no prototype: var obj = Object.create(null); or by using an object whose prototype is null as the object's prototype.)
However, somewhat tangentially, I wouldn't use that mechanism to check whether something is an array. In any modern environment, I'd use Array.isArray; and I'd shim/polyfill it on older environments. This also has the happy effect of working with arrays from other windows.
undefined or null do not have constructors and will error if you try to get it. Just try it out on Ctrl+Shift+I, and play around with it. Though, I doubt your if statement would cause much trouble, and it'd most likely error on pratical use.
You do NOT need to check length or anything, only see if it exists (or if you want specifically array, you could check that aswell).
Let's say that I have an object reference as follows in coffeescript:
object.param.foo.bar
and I want to validate that bar exists? How do I do that?
I believe that object.param.foo?.bar tests for the existence of foo, returning undefined if it does not exist, but that object.param.foo.bar? converts the entire expression into a boolean test.
What I want is something that returns object.param.foo.bar if it exists and undefined if it does not.
I think you're looking for the expression
object.param?.foo?.bar
which assumes that object exists, and will return undefined if either the .param, .foo, or .bar properties don't exist. You can omit ?s if you know they do exist.
I'm not sure I understand your question (how does object.param.foo?.bar not do what you want? you haven't told us.).
But if you want to perform null checks the whole way (in case object, object.param or object.param.bar is null), you can use:
bar = object?.param?.foo?.bar
This will produce undefined if anything along this chain is null or undefined, and the value of object.param.foo.bar if it exists.
I get the following error:
Uncaught TypeError: Object #<an Object> has no method 'indexOf'
for this line:
else if(file.indexOf(".mp3") == file.length - 4 && return_path === false)
What does this mean? Which is the object? What is a method? And what would be am simple example of an applied method? I'm really new to JavaScript so please keep it simple.
What does this mean?
It simply means that whatever type file is at the moment, it doesn't have a .indexOf() method, in this case that means it's not the string you think it is/should be.
Wich is the object? file
What is a method? 0 like any other language, objects have methods, .indexOf() is what it's trying to run here.
And what would be am simple example of an apllied method? I'm really new to javascript so please keep it simple.
I'm not sure what you mean by "applied method", but everything here just resolved around calling a method on an object, for example file.split(".") to turn it into an array, etc...any method String has.
var a = window.a || {};
It means a will be assigned window.a if it is not null or undefined, otherwise, it will equal an empty object
To answer the unasked question: this is used to make sure "a" will be valid.
Without it, when calling a.someFieldHere you might get exception saying "a is undefined", with such code in place you won't get such error.
It's useful when "a" is created elsewhere in some other code that not always get executed.
Kind of insurance policy. :)