How to code more efficiently in JavaScript

Islom Mashanlo
4 min readOct 2, 2020

JavaScript, often abbreviated as JS, is a high-level, often just-in-time compiled, and multi-paradigm programming language. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functions

JS is the industry standard for web development, from tech giants to junior level developers utilize JavaScript’s extremely flexible and versatile scripting capabilities. It allows developers to make dynamic websites with highly unique features by creating animations, interactive forms and more. However, all this flexibility and scalability comes with a caveat; which is that developers have to write a lot of basic repetitive code that sucks up a lot of their time. Therefore, I have decided to compile a list of libraries that will make your coding a lot of faster.

UnderscoreJS

Underscore is probably one of the most popular JS libraries that creates a plethora of useful functions that allows you to manipulate objects, collections, arrays and more. It has an easy understandable syntax that can help you code much faster.

// Underscore.JSlet sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
=> 6

The above code looks very similar…

#Ruby(1..3).reduce(:+)                             #=> 6

The syntax, for those who know Ruby syntax, should be very familiar and if you look up more methods that Underscore creates you will see a lot more similarity. Though some JS developers hate Underscore as much as they did jQuery, UnderscoreJS is still alive and will provide efficiency to a lot more coders.

MomentJS

Moment another extremely popular JS library that provides with a lot of functions that allow developers to manipulate Date-Time using intuitive functions. It provides with formatting functions, relative time functions, calendar time functions and it also provides with multiple language support.

// Pure JSfunction formatDate(date) {
let d = new Date(date),
month = '' + (d.getMonth() + 1),
day = '' + d.getDate(),
year = d.getFullYear();

if (month.length < 2)
month = '0' + month;
if (day.length < 2)
day = '0' + day;

return [year, month, day].join('/');
}
// 10/02/2020

Now let us use Moment

// Moment.JSmoment().format('L');    // 10/02/2020

As you can see, Moment will provide enormous amount of efficiency for those who do not desire to write every single basic functions. And most of Moment functions can be customized if you so desire.

ChartJS

Do you like graphs and charts as much as I do? But are you also very lazy? Then ChartJS is a library for you. It is a very minimalist library that makes your charts come to life. ChartJS provides you with 8 different chart types, animations for those charts and it is compatible with all modern web browsers as it uses HTML canvass and not any external dependancies.

See an example.

ChartJS provides will help you set up dynamic and professional looking websites that will help your visitors to visualize data and make your website aesthetically pleasing.

MathJS

Lastly we will talk about MathJS. It is an extremely useful library for developers who do not want the hassle of writing their own mathematical equations and logics. MathJS has an extensive library of functions that perform calculations from basic algebra to integral calculus. It also provides developers with the ability to utilize functions that help with statistical calculations; from medians to standard deviations.

Let us see how we would approach finding a median in an array of numbers.

// Pure JS Medianconst median = arr => {
const mid = Math.floor(arr.length / 2),
nums = [...arr].sort((a, b) => a - b);
return arr.length % 2 !== 0 ? nums[mid] : (nums[mid - 1] + nums[mid]) / 2;
};

And now let us use MathJS

// Math.JSmath.median(arr)

As you can observe, MathJS will make your life much easier by compacting a lot of these multiline functions into single lines of code.

Practice your skills

However, before you go and download all these libraries, you have to make sure that you understand the underlying code of all these libraries and make sure that your project collaborators are okay with you using these libraries. Many JavaScript programmers do not like these libraries as they prefer the freedom and flexibility that JS provides, so, once again, check in with your partners. Moreover, practice writing your own helper functions. Start with an easy average or sum function. And little by little start to abstract away more complex functions that will be called repeatedly. Abstraction is your friend and demonstrates a thorough understanding of JavaScript syntax, so please abstract away!

--

--