The Rise of Vanilla JavaScript: Why Plain JS is Making a Comeback

Posted: 06.01.2024

Recently we’ve noticed that plain JavaScript, often referred to as Vanilla JS, is (again) gaining significant traction within the developer community.

Modern JavaScript has evolved considerably from its early days, bringing many features that previously required heavy frameworks like jQuery, Lodash, React, and Angular. Let’s delve into a few key points to understand why Vanilla JS is resurging in popularity.

Performance

Performance is the critical factor that never stops being a priority. It directly influences how users perceive your website and has a proven impact on sales and conversions. Search engines, including Google, now incorporate real user insights and tools like Lighthouse to calculate search rankings and placements. Optimizing your website’s performance with Vanilla JS can lead to faster load times and a more responsive user experience. For more tips, check out our article on achieving perfect Lighthouse scores on your existing website. [Insert Link Here]

Do You Still Need jQuery?

A question often asked is, do you even need jQuery anymore?

While the debate continues on whether jQuery should be classified as a framework or a library, the more pertinent question is its necessity in modern web development. During the early Web 2.0 era, jQuery was invaluable, providing better selectors and simplifying Ajax requests. However, the landscape has changed. Today, browsers natively support complex selector queries with methods like querySelector() and querySelectorAll(), which are now powerful and efficient. The time has come to embrace native JavaScript and move beyond the training wheels that jQuery provided.

Moving Beyond Lodash

Lodash, like jQuery, served an essential role by offering utility functions for array and object manipulation that browsers didn’t support natively. But JavaScript has caught up. Many of Lodash’s functionalities are now built directly into the language. For instance, the spread operator ... simplifies many operations. Instead of looping through an array to push items, you can now use Array.push(...myArray). This not only makes the code cleaner but also more efficient.

Modern Event Listeners

One of the areas where Vanilla JS truly shines is in handling events. With concise methods to query elements and add event listeners, native JavaScript makes it straightforward to handle user interactions. For example:

document.querySelector('#myButton').addEventListener('click', () => {
  console.log('Button clicked!');
});

This simple yet powerful approach eliminates the need for additional libraries and keeps your codebase lightweight.

Using npm Build Process Tools

To further enhance the development process, tools like Webpack and Gulp can be used to compile, verify, and optimize JavaScript code. These build tools help bundle JavaScript files into minified and obfuscated versions, improving load times and performance.

Webpack:

Webpack is a powerful module bundler for JavaScript applications. It processes your application and produces bundles that are optimized for performance.

module.exports = {
  entry: './src/index.js',
  output: {
  filename: 'bundle.js',
    path: __dirname + '/dist'
  },
  mode: 'production'
};

Gulp:

Gulp is a toolkit for automating time-consuming tasks in your development workflow. It can be used to minify and obfuscate JavaScript files, among other things.

const gulp = require('gulp');
const uglify = require('gulp-uglify');
const rename = require('gulp-rename');

gulp.task('minify-js', function() {
  return gulp.src('src/*.js')
    .pipe(uglify())
    .pipe(rename({ suffix: '.min' }))
    .pipe(gulp.dest('dist'));
});

gulp.task('default', gulp.series('minify-js'));

Using these tools, you can ensure that your JavaScript code is optimized for production, resulting in faster, more efficient websites.

Conclusion

The comeback of Vanilla JS is a testament to the advancements in the language and browser capabilities. By leveraging modern JavaScript features and build tools like Webpack and Gulp, developers can create fast, efficient, and maintainable code without relying on bulky frameworks. Embracing Vanilla JS not only improves performance but also aligns with the current trend towards more streamlined and modular web development practices.