DEV Community

Cover image for 5 useful javascript tricks for begginers.

5 useful javascript tricks for begginers.

Minhazur Rahman Ratul on February 20, 2021

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...
Collapse
 
danim47c profile image
Daniel Mateos •

I think the best way you can obtain the most lengthy string in an array is this:

const mostLengthy = someArray
    .reduce((acc, i) =>
        i.length > acc.length
             ? i
             : acc
)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
coderslang profile image
Coderslang: Become a Software Engineer •

That's a good one, although it might be useful to first filter out nulls and undefineds from the array:

const mostLengthy = someArray
    .filter(item => item)
    .reduce((acc, i) => i.length > acc.length ? i : acc);
Enter fullscreen mode Exit fullscreen mode
Collapse
 
mdegoys profile image
Matthieu • • Edited

You can also do this way I believe (sort by the length of the words in descending order, then pick the first one).

const mostLengthy = someArray.sort((x, y) => y.length - x.length)[0] 
Enter fullscreen mode Exit fullscreen mode

Maybe it's more understandable ? But I agree reduce is great :).

Collapse
 
developeratul profile image
Minhazur Rahman Ratul •

useful :)

Collapse
 
nibelune profile image
Benoît Schiex • • Edited

last_Item = arr.slice(-1) would not return the last item but a new array containing the last item.

you can do :

const lastItem = arr[arr.length-1]
const lastItem = arr.slice(-1)[0]
const lastItem = arr.slice(-1).pop()
const [lastItem] = arr.slice(-1)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
developeratul profile image
Minhazur Rahman Ratul •

wow nice. !

Collapse
 
jonrandy profile image
Jon Randy 🎖️ •
const last_item = array.reverse()[0]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
kelsny profile image
Kelly •

that one mutates the array, though

Collapse
 
jonrandy profile image
Jon Randy 🎖️ •

Yeah. I always wondered why they decided to make it like that

Collapse
 
vbubblery profile image
Juncheng • • Edited

Try with this:

const number = 23.6565
console.log(~~number);
const string = "24"
console.log(+24)
Enter fullscreen mode Exit fullscreen mode
Collapse
 
Sloan, the sloth mascot
Comment deleted
Collapse
 
nibelune profile image
Benoît Schiex •

This is a bitwise or.
Since bitwise operations only make sense on integers, the decimal part is truncated.

Collapse
 
captainofphb profile image
CaptainOfPhB •

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.

Collapse
 
coderdeepansh profile image
Deepansh Dash •

Thanks it is useful.

Collapse
 
ianwijma profile image
Ian Wijma •

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!!

Collapse
 
baikai223 profile image
baikai223 •

获取数组的最后一项数据使用数组的api方法不是更好吗?arr.pop()

Collapse
 
developeratul profile image
Minhazur Rahman Ratul •

I don't understand what you have said :|

Collapse
 
baikai223 profile image
baikai223 •

Wouldn't it be better to use pop() instead of slice()?

Thread Thread
 
developeratul profile image
Minhazur Rahman Ratul •

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. :)

Collapse
 
aspiiire profile image
Aspiiire •

That is really useful, thank you 😁

Collapse
 
rocksuvel profile image
suvel rathneswar⚛️ •

Helpful ☺️

Collapse
 
devendra0110 profile image
Devendra Gaud •

I'm still wondering how the logic works in Trick-2.

Collapse
 
developeratul profile image
Minhazur Rahman Ratul •

Small trick ;)

Collapse
 
devendra0110 profile image
Devendra Gaud •

Ok, I just discovered two other ways similar to that trick.

console.log(~~5.95) 
console.log(5.95 >> 0)
Enter fullscreen mode Exit fullscreen mode

And I'm more confused now.

Thread Thread
 
developeratul profile image
Minhazur Rahman Ratul •

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()

Collapse
 
kamalhossain profile image
Kamal Hossain •

thanks for sharing.

Collapse
 
fabioluciano profile image
Fábio Luciano •

By normal number, did you mean integers?

Some comments have been hidden by the post's author - find out more