(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
Additional excellent recommendation by @GameAlchemist @GameAlchemist, verbatim:
- for optimisation purposes, the
if
is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Evenif (element)
is better writtenif (element != null)
(which makes obvious the defensive check againstgetElementById
returningnull
)
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
Additional excellent recommendation by @GameAlchemist, verbatim:
- for optimisation purposes, the
if
is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Evenif (element)
is better writtenif (element != null)
(which makes obvious the defensive check againstgetElementById
returningnull
)
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
Additional excellent recommendation by @GameAlchemist, verbatim:
- for optimisation purposes, the
if
is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Evenif (element)
is better writtenif (element != null)
(which makes obvious the defensive check againstgetElementById
returningnull
)
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
Additional excellent recommendation by @GameAlchemist , verbatim:
- for optimisation purposes, the
if
is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Evenif (element)
is better writtenif (element != null)
(which makes obvious the defensive check againstgetElementById
returningnull
)
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
Additional excellent recommendation by @GameAlchemist , verbatim:
- for optimisation purposes, the
if
is faster. and more importantly 2) a piece of code should be as much self-documenting as possible. Evenif (element)
is better writtenif (element != null)
(which makes obvious the defensive check againstgetElementById
returningnull
)
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
There's nothing wrong withIn this code there is a pointless assignment when the first oneelement is not found. It'sIn this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.
Your use case is very different from this example:
function gas(amount) { amount = amount || 10; this.mph += amount; return this; };
Here, ||
is used to set a default alternative.
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
To understand what's happening here, the quote you included in your post is very helpful:
Even primitive types get the object treatment when you refer to them with the property access notations. They get automatically wrapped with an object so that you can call their prototype methods.
What happens in this use case,
if the element doesn't exist with id arr[i].id
,
0 will be used as a default.
This gets wrapped with an object,
and (0).value = ...
becomes a valid expression.
In other words, if the id doesn't exist, the expression becomes a throw-away assignment.
This is more than just a shortcut: it's a dirty hack. Don't use it.
There's nothing wrong with the first one. It's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but confusing / obscure.
Your use case is very different from this example:
function gas(amount) { amount = amount || 10; this.mph += amount; return this; };
Here, ||
is used to set a default alternative.
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
To understand what's happening here, the quote you included in your post is very helpful:
Even primitive types get the object treatment when you refer to them with the property access notations. They get automatically wrapped with an object so that you can call their prototype methods.
What happens in this use case,
if the element doesn't exist with id arr[i].id
,
0 will be used as a default.
This gets wrapped with an object,
and (0).value = ...
becomes a valid expression.
In other words, if the id doesn't exist, the expression becomes a throw-away assignment.
This is more than just a shortcut: it's a dirty hack. Don't use it.
(document.getElementById(arr[i].id) || 0).value = arr[i].value;
In this code there is a pointless assignment when the element is not found. In this case, the 0 gets wrapped into a temporary object, and the assignment will work, but it will be pointless, it's result will be thrown away. This assignment is merely a side effect of the shortcut, it has no purpose of its own.
Every operation in a program should be intentional, purposeful, and this shortcut violates that. As such, I consider this a dirty hack. Don't use it.
When in doubt, go for the more intuitive and readable solution. The first example in your post is slightly longer, but it's simple and clear: there's no question about what will happen.
The second way, although shorter, may not be obvious for everyone.
Keep in mind that code is read far more times than it’s written. It's better to favor a technique that's clear and easy to understand to something "clever" but potentially confusing / obscure.