Javascript best practicesđ„
source link: https://dev.to/devsimc/javascript-best-practices-44ll
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
- Declare and Initialize Arrays in javascript
- Find out the sum, minimum and maximum value in javascript
- Sorting Array of String, Numbers or Objects in javascript
- Remove Duplicates array values in javascript
- Create a Counter Object or Map in javascript
- Ternary Operator in javascript
- Arrow Functions in javascript
- Shuffle an Array in javascript
- Rest & Spread operators in javascript
- Convert Decimal to Binary or Hexa in javascript
1. Declare and Initialize Arrays in javascript
We can initialize array of particular size with default values like "", null or 0. You might have used these for the 1-D array but how about initializing 2-D array/matrix?
const array = Array(5).fill('');
// Output
(5) ["", "", "", "", ""]
const matrix = Array(5).fill(0).map(()=>Array(5).fill(0));
// Output
(5) [Array(5), Array(5), Array(5), Array(5), Array(5)]
0: (5) [0, 0, 0, 0, 0]
1: (5) [0, 0, 0, 0, 0]
2: (5) [0, 0, 0, 0, 0]
3: (5) [0, 0, 0, 0, 0]
4: (5) [0, 0, 0, 0, 0]
length: 5
2. Find out the sum, minimum and maximum value in javascript
const array = [5,4,7,8,9,2];
Sum in array javascript
array.reduce((a,b) => a+b);
// Output: 35
MAX in array javascript
array.reduce((a,b) => a>b?a:b);
// Output: 9
MIN in array javascript
array.reduce((a,b) => a<b?a:b);
// Output: 2
3. Sorting Array of String, Numbers or Objects in javascript
const stringArr = ["Joe", "Kapil", "Steve", "Musk"]
stringArr.sort();
// Output
(4) ["Joe", "Kapil", "Musk", "Steve"]
stringArr.reverse();
// Output
(4) ["Steve", "Musk", "Kapil", "Joe"]
### Sort Number Array in javascript
const array = [40, 100, 1, 5, 25, 10];
array.sort((a,b) => a-b);
// Output
(6) [1, 5, 10, 25, 40, 100]
array.sort((a,b) => b-a);
// Output
(6) [100, 40, 25, 10, 5, 1]
4. Remove Duplicates array values in javascript
const array = [5,4,7,8,9,2,7,5];
array.filter((item,idx,arr) => arr.indexOf(item) === idx);
// or
const nonUnique = [...new Set(array)];
// Output: [5, 4, 7, 8, 9, 2]
5. Create a Counter Object or Map in javascript
let string = 'kapilalipak';
const table={};
for(let char of string) {
table[char]=table[char]+1 || 1;
}
// Output
{k: 2, a: 3, p: 2, i: 2, l: 2}
6. Ternary Operator in javascript
function Fever(temp) {
return temp > 97 ? 'Visit Doctor!'
: temp < 97 ? 'Go Out and Play!!'
: temp === 97 ? 'Take Some Rest!';
}
// Output
Fever(97): "Take Some Rest!"
Fever(100): "Visit Doctor!"
Find Out More Tips on main source of an article.
Recommend
-
6
Variables are everywhere. Even if youâre writing a small function, or writing an application: you always declare, assign, and read variables. Writing quality variables brings increased readability and easier maintainability of your c...
-
17
Introduction Writing a best practice article is quite a tricky business. To a number of you, what you are about to read will appear to be very obvious and just the sensible thing to do. However, looking around the web and gett...
-
12
â All postsJavaScript best practices to improve code qualityLearn how some of the new features in JavaScript can help you write cleaner code.By Dhruv on April 7, 20...
-
1
by Jeffrey Way16 Jun 2009Difficulty:BeginnerLength:LongLanguages: As a follow-up to
-
5
Best Practices to Measure Execution Time in JavaScriptJavaScript / August 31, 2021
-
3
Questions What is the correct/acceptable way to access all JavaScript/TypeScript functions in a module for unit testing? Is there a (good) reason why I shouldn't just export all my functions? Context...
-
6
JavaScript security: Vulnerabilities and best practices By Anna Monus | Posted Dec 15, 2021 | 9 min. (1737 words) If you run an interactive website or application, Jav...
-
4
Best Practices When Using JavaScript in Development Categories Risk ManagementPublished on July 21,...
-
7
JavaScript UI Testing: Best Practices published on 12 August 2022 The quality of the UI can make or break a web application. The first impression the user gets is cruci...
-
2
6 Best Practices to Design Javascript Coding Challenges with APIs ...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK