In this post, I will show you 5 awesome javascript tricks which will make your life more easier. And will help you to become a better developer. So...
Some comments have been hidden by the post's author - find out more
For further actions, you may consider blocking this person and/or reporting abuse
I think the best way you can obtain the most lengthy string in an array is this:
That's a good one, although it might be useful to first filter out
null
s andundefined
s from the array:You can also do this way I believe (sort by the length of the words in descending order, then pick the first one).
Maybe it's more understandable ? But I agree
reduce
is great :).useful :)
last_Item = arr.slice(-1) would not return the last item but a new array containing the last item.
you can do :
wow nice. !
that one mutates the array, though
Yeah. I always wondered why they decided to make it like that
Try with this:
This is a bitwise or.
Since bitwise operations only make sense on integers, the decimal part is truncated.
I think those are basic skills of javascript developer(maybe you are a new bee), and the second trick is not recommended, Math.round/Math.floor/Math.ceil are better choice, which will make your code more readable.
That is really useful, thank you 😁
获取数组的最后一项数据使用数组的api方法不是更好吗?arr.pop()
I don't understand what you have said :|
Wouldn't it be better to use pop() instead of slice()?
you can use what ever you want. ......I know how to do that with slice so I have shown with slice. If you know how to do that with pop() you can use pop. :)
Thanks it is useful.
Not 100% sure you should the weird int casting. Not everyone knows about it. And could cause confusion when reading it. Maybe just use parseInt. 😁 Really Cool post though!!
thanks for sharing.
By normal number, did you mean integers?
I'm still wondering how the logic works in Trick-2.
Small trick ;)
Ok, I just discovered two other ways similar to that trick.
And I'm more confused now.
you can use what ever you want. Both of the tricks does the same thing. Or you can also use Math.floor() or Math.round()
Helpful ☺️