Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
June 8, 2022 01:42 pm GMT

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

Almost end of this tips for JS developer must know.
Today is 8th part and I will explain about Commas, Semicolons, Type Casting & Coercion.

1. Commas
Leading commas: Nope.

// badconst story = [    once  , upon  , aTime];// goodconst story = [  once,  upon,  aTime,];// badconst hero = {    firstName: 'Ada'  , lastName: 'Lovelace'  , birthYear: 1815  , superPower: 'computers'};// goodconst hero = {  firstName: 'Ada',  lastName: 'Lovelace',  birthYear: 1815,  superPower: 'computers',};

Additional trailing comma: Yup

// bad - git diff without trailing commaconst hero = {     firstName: 'Florence',-    lastName: 'Nightingale'+    lastName: 'Nightingale',+    inventorOf: ['coxcomb chart', 'modern nursing']};// good - git diff with trailing commaconst hero = {     firstName: 'Florence',     lastName: 'Nightingale',+    inventorOf: ['coxcomb chart', 'modern nursing'],};
// badconst hero = {  firstName: 'Dana',  lastName: 'Scully'};const heroes = [  'Batman',  'Superman'];// goodconst hero = {  firstName: 'Dana',  lastName: 'Scully',};const heroes = [  'Batman',  'Superman',];// badfunction createHero(  firstName,  lastName,  inventorOf) {  // does nothing}// goodfunction createHero(  firstName,  lastName,  inventorOf,) {  // does nothing}// good (note that a comma must not appear after a "rest" element)function createHero(  firstName,  lastName,  inventorOf,  ...heroArgs) {  // does nothing}// badcreateHero(  firstName,  lastName,  inventorOf);// goodcreateHero(  firstName,  lastName,  inventorOf,);// good (note that a comma must not appear after a "rest" element)createHero(  firstName,  lastName,  inventorOf,  ...heroArgs);

2. Semicolons
Yup. eslint: semi

// bad - raises exceptionconst luke = {}const leia = {}[luke, leia].forEach((jedi) => jedi.father = 'vader')// bad - raises exceptionconst reaction = "No! Thats impossible!"(async function meanwhileOnTheFalcon() {  // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`  // ...}())// bad - returns `undefined` instead of the value on the next line - always happens when `return` is on a line by itself because of ASI!function foo() {  return    'search your feelings, you know it to be foo'}// goodconst luke = {};const leia = {};[luke, leia].forEach((jedi) => {  jedi.father = 'vader';});// goodconst reaction = "No! Thats impossible!";(async function meanwhileOnTheFalcon() {  // handle `leia`, `lando`, `chewie`, `r2`, `c3p0`  // ...}());// goodfunction foo() {  return 'search your feelings, you know it to be foo';}

3. Type Casting & Coercion
Perform type coercion at the beginning of the statement.

Strings: eslint: no-new-wrappers

// => this.reviewScore = 9;// badconst totalScore = new String(this.reviewScore); // typeof totalScore is "object" not "string"// badconst totalScore = this.reviewScore + ''; // invokes this.reviewScore.valueOf()// badconst totalScore = this.reviewScore.toString(); // isnt guaranteed to return a string// goodconst totalScore = String(this.reviewScore);

Numbers: Use Number for type casting and parseInt always with a radix for parsing strings.

const inputValue = '4';// badconst val = new Number(inputValue);// badconst val = +inputValue;// badconst val = inputValue >> 0;// badconst val = parseInt(inputValue);// goodconst val = Number(inputValue);// goodconst val = parseInt(inputValue, 10);

If for whatever reason you are doing something wild and parseInt is your bottleneck and need to use Bitshift for performance reasons, leave a comment explaining why and what youre doing.

// good/** * parseInt was the reason my code was slow. * Bitshifting the String to coerce it to a * Number made it a lot faster. */const val = inputValue >> 0;

Note: Be careful when using bitshift operations. Numbers are represented as 64-bit values, but bitshift operations always return a 32-bit integer (source). Bitshift can lead to unexpected behavior for integer values larger than 32 bits. Discussion. Largest signed 32-bit Int is 2,147,483,647:

2147483647 >> 0; // => 21474836472147483648 >> 0; // => -21474836482147483649 >> 0; // => -2147483647

Booleans

const age = 0;// badconst hasAge = new Boolean(age);// goodconst hasAge = Boolean(age);// bestconst hasAge = !!age;

Thanks for your time.


Original Link: https://dev.to/stormytalent/50-hints-jses6-developer-must-know-8th-part-6nb

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