DEV Community

Cover image for πŸ€“7 Useful JavaScript One-Liner Code You Must Know πŸ’Ž

πŸ€“7 Useful JavaScript One-Liner Code You Must Know πŸ’Ž

Random on December 06, 2023

Hey Coders, it's me Md Taqui Imam. In today's blog, I want to share some 7 useful JavaScript one-liner code that can make your code look more profe...
Collapse
 
ranjancse profile image
Ranjan Dailata • Edited

Improved code for deep cloning of objects.

function deepClone(obj) {
  // Check if the input is an object
  if (obj === null || typeof obj !== 'object') {
    return obj;
  }

  return JSON.parse(JSON.stringify(obj));
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
random_ti profile image
Random

Thankyou Ranjan for your contribuition πŸ™

Collapse
 
jonrandy profile image
Jon Randy πŸŽ–οΈ

Further improved code - use the native structuredClone:

const clone = structuredClone(obj)
Enter fullscreen mode Exit fullscreen mode

Faster, less problems, more options.

Thread Thread
 
random_ti profile image
Random

this is πŸ”₯

Collapse
 
joolsmcfly profile image
Julien Dephix

Hi.

You can add js next to the opening backticks to have syntax highlight.

Also note that JSON.stringify will work as you'd expect if properties are booleans, numbers etc. Functions, Maps, Sets will be omitted or null'd.

Check the doc for more info.

Collapse
 
random_ti profile image
Random

Thankyou so much julien, i really don't know about this js syntax feature of dev to.

Collapse
 
joolsmcfly profile image
Julien Dephix • Edited

It can be any supported language too.

<p class="greeting">Hello from HTML</p>
Enter fullscreen mode Exit fullscreen mode
echo "Hello from PHP";
Enter fullscreen mode Exit fullscreen mode

etc

Collapse
 
jeffchavez_dev profile image
Jeff Chavez

Thank you. For #5, Is it dublicate or duplicate?

Collapse
 
random_ti profile image
Random

Sorry its duplicate πŸ˜… Thankyou for informing me

Collapse
 
jeffchavez_dev profile image
Jeff Chavez

You're welcome

Collapse
 
devluc profile image
Devluc

Awesome tips Taqui, thanks for sharing them

Collapse
 
milanx profile image
Milan Mohapatra

Looking coolπŸ”₯, how can I add buttons and follow me sections like you.

Collapse
 
random_ti profile image
Random • Edited

It is called CTA(call to action) button.

This is the syntax to add CTA πŸ‘‡ Button

{% cta https://example.com %} Follow me in Githubβœ…{% endcta %}
Enter fullscreen mode Exit fullscreen mode

Replace "example.com" and "Follow me in Github" accourding to you.

😊