Why I can do if else shorthand
inside .push()
function ? like
var arr = [];
arr.push(test||null);
// nothing
But
var arr = [];
var test = test||null;
arr.push(test);
// [null]
I need to insert null
if variable is undefined.
Why I cant use test||null
inside .push()
function ?
The arr. push() method is used to push one or more values into the array. This method changes the length of the array by the number of elements added to the array.
The push() method adds one or more elements to the end of an array and returns the new length of the array.
JavaScript Array push()The push() method adds new items to the end of an array. The push() method changes the length of the array. The push() method returns the new length.
Unshift is slower than push because it also needs to unshift all the elements to the left once the first element is added.
arr.push(typeof(test) == "undefined" ? null: test);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With