Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 28, 2022 12:39 am GMT

50 hints JS(ES6) developer must know (5th part)

Hi, everyone! How are you doing?
Here is StormyTalent again to explain about the 50+ critical JS hints.
Today we're gonna see about Modules, Properties, Variables.

Always use modules (import/export) over a non-standard module system. You can always transpile to your preferred module system.

Modules

const numbers = [1, 2, 3, 4, 5];// badlet sum = 0;for (let num of numbers) {  sum += num;}sum === 15;// goodlet sum = 0;numbers.forEach((num) => {  sum += num;});sum === 15;// best (use the functional force)const sum = numbers.reduce((total, num) => total + num, 0);sum === 15;// badconst increasedByOne = [];for (let i = 0; i < numbers.length; i++) {  increasedByOne.push(numbers[i] + 1);}// goodconst increasedByOne = [];numbers.forEach((num) => {  increasedByOne.push(num + 1);});// best (keeping it functional)const increasedByOne = numbers.map((num) => num + 1);

If you must use generators, or if you disregard our advice, make sure their function signature is spaced properly.

// badfunction * foo() {  // ...}// badconst bar = function * () {  // ...};// badconst baz = function *() {  // ...};// badconst quux = function*() {  // ...};// badfunction*foo() {  // ...}// badfunction *foo() {  // ...}// very badfunction*foo() {  // ...}// very badconst wat = function*() {  // ...};// goodfunction* foo() {  // ...}// goodconst foo = function* () {  // ...};

Properties

Use dot notation when accessing properties

const luke = {  jedi: true,  age: 28,};// badconst isJedi = luke['jedi'];// goodconst isJedi = luke.jedi;

Use bracket notation [] when accessing properties with a variable.

const luke = {  jedi: true,  age: 28,};function getProp(prop) {  return luke[prop];}const isJedi = getProp('jedi');

Use exponentiation operator ** when calculating exponentiations.

// badconst binary = Math.pow(2, 10);// goodconst binary = 2 ** 10;

Variables

Always use const or let to declare variables. Not doing so will result in global variables. We want to avoid polluting the global namespace. Captain Planet warned us of that.

// badsuperPower = new SuperPower();// goodconst superPower = new SuperPower();

Use one const or let declaration per variable or assignment.

// badconst items = getItems(),    goSportsTeam = true,    dragonball = 'z';// bad// (compare to above, and try to spot the mistake)const items = getItems(),    goSportsTeam = true;    dragonball = 'z';// goodconst items = getItems();const goSportsTeam = true;const dragonball = 'z';

Group all your consts and then group all your lets.

// badlet i, len, dragonball,    items = getItems(),    goSportsTeam = true;// badlet i;const items = getItems();let dragonball;const goSportsTeam = true;let len;// goodconst goSportsTeam = true;const items = getItems();let dragonball;let i;let length;

Assign variables where you need them, but place them in a reasonable place.

// bad - unnecessary function callfunction checkName(hasName) {  const name = getName();  if (hasName === 'test') {    return false;  }  if (name === 'test') {    this.setName('');    return false;  }  return name;}// goodfunction checkName(hasName) {  if (hasName === 'test') {    return false;  }  const name = getName();  if (name === 'test') {    this.setName('');    return false;  }  return name;}

Dont chain variable assignments.

// bad(function example() {  // JavaScript interprets this as  // let a = ( b = ( c = 1 ) );  // The let keyword only applies to variable a; variables b and c become  // global variables.  let a = b = c = 1;}());console.log(a); // throws ReferenceErrorconsole.log(b); // 1console.log(c); // 1// good(function example() {  let a = 1;  let b = a;  let c = a;}());console.log(a); // throws ReferenceErrorconsole.log(b); // throws ReferenceErrorconsole.log(c); // throws ReferenceError// the same applies for `const`

Avoid using unary increments and decrements (++, --)

// badconst array = [1, 2, 3];let num = 1;num++;--num;let sum = 0;let truthyCount = 0;for (let i = 0; i < array.length; i++) {  let value = array[i];  sum += value;  if (value) {    truthyCount++;  }}// goodconst array = [1, 2, 3];let num = 1;num += 1;num -= 1;const sum = array.reduce((a, b) => a + b, 0);const truthyCount = array.filter(Boolean).length;

Avoid linebreaks before or after = in an assignment. If your assignment violates max-len, surround the value in parens.

// badconst foo =  superLongLongLongLongLongLongLongLongFunctionName();// badconst foo  = 'superLongLongLongLongLongLongLongLongString';// goodconst foo = (  superLongLongLongLongLongLongLongLongFunctionName());// goodconst foo = 'superLongLongLongLongLongLongLongLongString';

Disallow unused variables.

// badvar some_unused_var = 42;// Write-only variables are not considered as used.var y = 10;y = 5;// A read for a modification of itself is not considered as used.var z = 0;z = z + 1;// Unused function arguments.function getX(x, y) {    return x;}// goodfunction getXPlusY(x, y) {  return x + y;}var x = 1;var y = a + 2;alert(getXPlusY(x, y));// 'type' is ignored even if unused because it has a rest property sibling.// This is a form of extracting an object that omits the specified keys.var { type, ...coords } = data;// 'coords' is now the 'data' object without its 'type' property.

Thanks for your time.


Original Link: https://dev.to/stormytalent/50-hints-jses6-developer-must-know-5th-part-4ham

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To