No Result
View All Result
CloudReports
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner
No Result
View All Result
CloudReports
No Result
View All Result
Home Javascript

6 useful tips for JavaScript Developers

npn by npn
September 22, 2019
in Javascript
Reading Time: 3 mins read
0
6 useful tips for JavaScript Developers
0
SHARES
145
VIEWS
Share on FacebookShare on Twitter

Contents

  • 1 READ ALSO
  • 2 Configuring VS Code for Node/JavaScript Development
  • 3 How does Nodejs solve the problem of high concurrency?
  • 4 1. Get Unique Values ​​in Array
  • 5 2. Array and Boolean
  • 6 3. Create Empty Objects
  • 7 4. Merge Objects
  • 8 5. Require Function Parameters
  • 9 6. Get Query String Parameters
Rate this post

Like every other programming language, JavaScript has dozens of tricks to perform both easy and difficult tasks. Some tips are widely known while others are enough to blow your mind. In this article, we will go over 7 Javascript tips that you can start practicing and using after you finish reading this article.

READ ALSO

Configuring VS Code for Node/JavaScript Development

Configuring VS Code for Node/JavaScript Development

August 2, 2021
809
How does Nodejs solve the problem of high concurrency?

How does Nodejs solve the problem of high concurrency?

July 18, 2021
888

1. Get Unique Values ​​in Array

Solving duplicate array elements using Set.

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

2. Array and Boolean

You need to filter falsy values (0, undefined, null, false, etc.) from an array? You may not know this trick:

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

3. Create Empty Objects

Of course, you can also create with {}. But you know the object just created that way still has the object hasOwnProperty, proto of that.

let obj = Object.create({});

// obj.__proto__ === {}
// obj.hasOwnProperty === function

and new tricks.

let dict = Object.create(null);

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

4. Merge Objects

You can never avoid merging objects in your developer life, so there are tricks for Javascript developer to achieve that.

ADVERTISEMENT

Using spread syntax:

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 " ,
}
* /

5. Require Function Parameters

Setting default values ​​for function arguments is a great addition to JavaScript, but see this tip to ask for values ​​passed to 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' );

6. Get Query String Parameters

// 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"

JavaScript has changed a lot over the years, but my favorite part of JavaScript today is the speed at which the language is being improved. Although JavaScript changes, we still need to use some good tips;
Keep these tips in your toolbox when you need them!

Tags: javascript tips
ShareTweetShare
Previous Post

Understanding and using ReactJs component in real project – ReactJs tutorial [P2]

Next Post

V8 Zero-cost async stack traces

npn

npn

Related Posts

Configuring VS Code for Node/JavaScript Development
Javascript

Configuring VS Code for Node/JavaScript Development

August 2, 2021
809
How does Nodejs solve the problem of high concurrency?
Javascript

How does Nodejs solve the problem of high concurrency?

July 18, 2021
888
Npm module: a backdoor and ambush questions
Javascript

Npm module: a backdoor and ambush questions

December 16, 2020
191
NPM: three packets contained malicious code
Javascript

NPM: three packets contained malicious code

December 16, 2020
73
25 years of JavaScript: the programming language that makes the world go round
Javascript

25 years of JavaScript: the programming language that makes the world go round

December 16, 2020
226
The story of migrating 70,000 lines of JavaScript code to TypeScript
Javascript

The story of migrating 70,000 lines of JavaScript code to TypeScript

December 15, 2020
280
Next Post
V8 Zero-cost async stack traces

V8 Zero-cost async stack traces

Discussion about this post

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)

Search

No Result
View All Result

Categories

  • Android (1)
  • Ant Design tutorial (7)
  • App/Game (2)
  • Javascript (16)
  • Layout and Routing (2)
  • Linux (9)
  • PC & LAPTOP (6)
  • PERSONAL FINANCES (1)
  • React (13)
  • SQL (2)
  • TECHNOLOGY & DIGITAL (7)
  • The Basics (5)
  • Web development (37)
No Result
View All Result
  • Home
  • Linux
  • Web development
  • Javascript
  • SQL
  • Ant Design tutorial
  • QR Code Scanner