Thursday, January 21, 2021
  • Setup menu at Appearance » Menus and assign menu to Top Bar Navigation
Advertisement
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News
No Result
View All Result
NikolaNews
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News
No Result
View All Result
NikolaNews
No Result
View All Result
Home Learn to Code

7 Useful JavaScript Tricks

April 16, 2019
in Learn to Code
How to Change the WordPress Admin Login Logo
585
SHARES
3.3k
VIEWS
Share on FacebookShare on Twitter
Credit: DavidWalsh

Just like every other programming language, JavaScript has dozens of tricks to accomplish both easy and difficult tasks. Some tricks are widely known while others are enough to blow your mind. Let’s have a look at {x} JavaScript tricks you can start using today!

Get Unique Values of an Array

Getting an array of unique values is probably easier than you think:

You might also like

navigator.clipboard API

Goals For 2021

Open a Browser Tab with DevTools Open by Default

var j = [...new Set([1, 2, 3, 3])]
>> [1, 2, 3]

I love the mixture of rest expression and Set!

Array and Boolean

Ever need to filter falsy values (0, undefined, null, false, etc.) out of an array? You may not have known this trick:

myArray
    .map(item => {
        // ...
    })
    // Get rid of bad values
    .filter(Boolean);

Just pass Boolean and all those falsy value go away!

Create Empty Objects

Sure you can create an object that seems empty with {}, but that object still has a __proto__ and the usual hasOwnProperty and other object methods. There is a way, however, to create a pure “dictionary” object:

let dict = Object.create(null);

// dict.__proto__ === "undefined"
// No object properties exist until you add them

There are absolutely no keys or methods on that object that you don’t put there!

Merge Objects

The need to merge multiple objects in JavaScript has been around forever, especially as we started creating classes and widgets with options:

const person = { name: 'David Walsh', gender: 'Male' };
const tools = { computer: 'Mac', editor: 'Atom' };
const attributes = { handsomeness: 'Extreme', hair: 'Brown', eyes: 'Blue' };

const summary = {...person, ...tools, ...attributes};
/*
Object {
  "computer": "Mac",
  "editor": "Atom",
  "eyes": "Blue",
  "gender": "Male",
  "hair": "Brown",
  "handsomeness": "Extreme",
  "name": "David Walsh",
}
*/

Those three dots made the task so much easier!

Require Function Parameters

Being able to set default values for function arguments was an awesome addition to JavaScript, but check out this trick for requiring values be passed for a given argument:

const isRequired = () => { throw new Error('param is required'); };

const hello = (name = isRequired()) => { console.log(`hello ${name}`) };

// This will throw an error because no name is provided
hello();

// This will also throw an error
hello(undefined);

// These are good!
hello(null);
hello('David');

That’s some next level validation and JavaScript usage!

Destructuring Aliases

Destructuring is a very welcomed addition to JavaScript but sometimes we’d prefer to refer to those properties by another name, so we can take advantage of aliases:

const obj = { x: 1 };

// Grabs obj.x as { x }
const { x } = obj;

// Grabs obj.x as as { otherName }
const { x: otherName } = obj;

Useful for avoiding naming conflicts with existing variables!

Get Query String Parameters

For years we wrote gross regular expressions to get query string values but those days are gone — enter the amazing URLSearchParams API:

// Assuming "?post=1234&action=edit"

var urlParams = new URLSearchParams(window.location.search);

console.log(urlParams.has('post')); // true
console.log(urlParams.get('action')); // "edit"
console.log(urlParams.getAll('action')); // ["edit"]
console.log(urlParams.toString()); // "?post=1234&action=edit"
console.log(urlParams.append('active', '1')); // "?post=1234&action=edit&active=1"

Much easier than we used to fight with!

JavaScript has changed so much over the years but my favorite part of JavaScript these days is the velocity in language improvements we’re seeing. Despite the changing dynamic of JavaScript, we still need to employ a few decent tricks; keep these tricks in your toolbox for when you need them!

What are your favorite JavaScript tricks?

Credit: DavidWalsh By: David Walsh

Previous Post

Google: Expect longer Android app review process for unknown developers

Next Post

How AI Is Redefining the Consumer Goods Industry – Becoming Human: Artificial Intelligence Magazine

Related Posts

How to Change the WordPress Admin Login Logo
Learn to Code

navigator.clipboard API

January 11, 2021
How to Change the WordPress Admin Login Logo
Learn to Code

Goals For 2021

December 29, 2020
How to Change the WordPress Admin Login Logo
Learn to Code

Open a Browser Tab with DevTools Open by Default

December 28, 2020
How to Change the WordPress Admin Login Logo
Learn to Code

Return a Default Value with Promises Using catch

December 23, 2020
How to Change the WordPress Admin Login Logo
Learn to Code

Break a forEach Loop with JavaScript

December 22, 2020
Next Post
How AI Is Redefining the Consumer Goods Industry – Becoming Human: Artificial Intelligence Magazine

How AI Is Redefining the Consumer Goods Industry – Becoming Human: Artificial Intelligence Magazine

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Recommended

Plasticity in Deep Learning: Dynamic Adaptations for AI Self-Driving Cars

Plasticity in Deep Learning: Dynamic Adaptations for AI Self-Driving Cars

January 6, 2019
Microsoft, Google Use Artificial Intelligence to Fight Hackers

Microsoft, Google Use Artificial Intelligence to Fight Hackers

January 6, 2019

Categories

  • Artificial Intelligence
  • Big Data
  • Blockchain
  • Crypto News
  • Data Science
  • Digital Marketing
  • Internet Privacy
  • Internet Security
  • Learn to Code
  • Machine Learning
  • Marketing Technology
  • Neural Networks
  • Technology Companies

Don't miss it

Skyrim modders have a new machine learning tool that turns text to realistic NPC speech
Machine Learning

Skyrim modders have a new machine learning tool that turns text to realistic NPC speech

January 21, 2021
6 Major AI Use Cases In IT Operations | by Gina Shaw | Jan, 2021
Neural Networks

6 Major AI Use Cases In IT Operations | by Gina Shaw | Jan, 2021

January 21, 2021
Agile Marketing: 3 Tips for a Post-Pandemic Economy
Marketing Technology

Agile Marketing: 3 Tips for a Post-Pandemic Economy

January 21, 2021
Best antivirus software in 2021
Internet Security

Best antivirus software in 2021

January 21, 2021
The 37 Best Machine Learning Courses on Udemy to Consider
Machine Learning

The 37 Best Machine Learning Courses on Udemy to Consider

January 21, 2021
Classifying employees as likely-to-quit using Tensorflow, Pandas & IBM attrition dataset | by Timilsinasandesh | Jan, 2021
Neural Networks

Classifying employees as likely-to-quit using Tensorflow, Pandas & IBM attrition dataset | by Timilsinasandesh | Jan, 2021

January 21, 2021
NikolaNews

NikolaNews.com is an online News Portal which aims to share news about blockchain, AI, Big Data, and Data Privacy and more!

What’s New Here?

  • Skyrim modders have a new machine learning tool that turns text to realistic NPC speech January 21, 2021
  • 6 Major AI Use Cases In IT Operations | by Gina Shaw | Jan, 2021 January 21, 2021
  • Agile Marketing: 3 Tips for a Post-Pandemic Economy January 21, 2021
  • Best antivirus software in 2021 January 21, 2021

Subscribe to get more!

© 2019 NikolaNews.com - Global Tech Updates

No Result
View All Result
  • AI Development
    • Artificial Intelligence
    • Machine Learning
    • Neural Networks
    • Learn to Code
  • Data
    • Blockchain
    • Big Data
    • Data Science
  • IT Security
    • Internet Privacy
    • Internet Security
  • Marketing
    • Digital Marketing
    • Marketing Technology
  • Technology Companies
  • Crypto News

© 2019 NikolaNews.com - Global Tech Updates