Some mundane things I learnt about JavaScript recently

May 05, 2018

JavaScript is a welcoming language - admittedly, one that takes much longer to master than it does to learn.

At work, I've been working on interesting bits and pieces which are helping me solidify my knowledge. Here are some interesting as well as mundane things I've gotten comfortable with recently.

window.print() is not a part of ECMAScript

Documentation

I was surprised to learn that window.print() is not a spec - only a Living Standard. From a cursory test, it seems Firefox for Android does not know what to do with window.print() whereas Chrome for Android does. If you wrap your website in an Android WebView or UIWebView/WKWebView, neither Android nor iOS will get you anywhere with a call to this function.

Android's developer documentation specifically states:

You cannot use JavaScript in a HTML document to trigger printing.

Object spread syntax to write flexible constructors and methods

Documentation

Consider:

const createShape = (config) => {
    return {
        length: 50,
        breadth: 30,
        {...config}
    };
};

let sqauare = createShape({ breadth: 50 });
let cube = createShape({ height: 75 });

You can add additional key-value pairs, or overrwrite an existing pair, or both!

It was a tweet by @wesbos that really solidified it as a pattern in my memory, despite having used it at work several times.

Your browser can find out a lot about your battery

Your browser lets anyone query what the status and charge level of your battery is. How cool and frightening is that?

JavaScript Navigator's getBattery

An interesting Atom keyboard shortcut

I love keyboard shortcuts - easing them into your mental map and muscle memory is a huge plus to your day-to-day productivity, without the stress of remembering 20 keyboard combinations all at once.

I chanced upon Ctrl + R which generates symbols and lets you look through or search just the methods in a file. This is what it looks like in a React file using ES6 classes:

Atom keyboard shortcut preview

Ruppal Singh

Front-end Web Developer

Of late,
I've been enjoying building simple & beautiful static websites, working on web-apps employing React, and taking on complementary tasks in between - ranging from deployments to writing email templates. Remote work is preferred - I am a (mostly) happy git user.

In the past,
I have worked with PHP, AngularJS, WordPress, and jQuery. A personal milestone was a hand-crafted admin panel to allow the client to perform various operations on a MySQL database, and send out customized email newsletters, using a graphical user interface - in addition to the extensive site itself.