In JavaScript, there may be times when you want to assign one variable to a non-null value. The JavaScript OR assignment operator can be used to qu...
For further actions, you may consider blocking this person and/or reporting abuse
You should replace "null" / "null or undefined" by "falsy".
And also have a look at the "??" operator.
Keep writing it is a good way to improve!
Yeah, you're correct, it is a "falsely" value that is evaluated, I didn't want to make this post too complicated, but great to mention this for the curious readers. Thanks
I am not sure what you mean by "??" operator, I have written a post on the ternary operator and I plan to write a post about the optional chaining operator ('?.') in the future.
Here is the related documentation:
developer.mozilla.org/en-US/docs/W...
Thanks, it seems like a more strict version of the OR assignment operator. Perhaps I shall create a post comparing the two
I think that you should point out the differences between falsy values (cf. developer.mozilla.org/en-US/docs/G...) and null/undefined checks (with ??). I think that will add value to your post.
I think I shall leave this post as it is for now and cover the falsy values and the coalescing operator in separate posts, thank you for the feedback and ideas
Up to you, I just recommend you to be careful with the terms that you use in your post and in my opinion it is a good to support your posts with official references.
For example you function:
Doesn't work if you pass to it a falsy value like 0 or an empty string.
By being not precise you will assume wrong things (I have been there so many times, and it is still happening).
I would recommend the "you don't know js" by Kyle Simpson (@getify ), to expand your knowledge.
Thanks for all the feedback. My plan isn't to fill every post with all the detail needed, at least not right now (I don't have the time to put that much time into each post). I mainly want to encourage people to begin to learn how to code, or experiment with something they haven't used before. I don't admit to knowing everything, but I don't want to overload the reader with too much information that reduces their interest in code. Again, thank you for all the feedback, I super appreciate it and I guarantee some readers will benefit from some of the links you've posted.
You’re talking about the regular OR-operator; the OR assignment-operator doesn’t exist in Javascript (unfortunately)
I consider the standard or operator the one used in if statement, for boolean logic. Then the or assignment operator being used to assign values to variables (assign this value, if its falsey assign this value instead). I could be completely wrong in my phrasing, but I think it makes a nice distinction between the two use cases
You can consider a melon a nail when you hit it with a hammer, but it’s not going to make sense to anybody.
I’m afraid the long and short of it is just that the OR-assignment operator (
||=
) is not the OR-operator (||
), sorry man.Okay, thanks for the clarification
Okay thank you
Great stuff, do you think my explanation in this post is good?