Javascript ++: How to Level Up Your Code

Image for post
Image for post

Type Constructors are slower than declaring the object type as seen in the bottom three examples below. Constructors also create objects instead of primitive value so you can get some nasty side effects.

let x1 = new Object();
let x2 = new String();
let x3 = new Number();
let a1 = {};
let a2 = "";
let a3 = 0;

Abstract Equality Comparison (==)

"42" == 42
=> true

Abstract Comparators perform a type conversion when comparing so if you try to compare a string with a number, you might find yourself with a true value instead of a false one.

Strict Equality Comparison (===)

"42" === 42
=> false

Strict Comparators don’t do a type conversion so if the two objects you are comparing aren’t both integers (for example) with the same value, then you will get a false output since, well, these two objects aren’t the same thing!

I know what you’re thinking, “what!?” But passing in lots of arguments can actually benefit you. Don’t ever be afraid to add more than one, two, or even three arguments! Plus, the order of the arguments inside your function doesn’t matter, so you could even use the first variable at the very end of your function.

function sayHello(options) {
let sentence = {
greeting: "Good morning!",
pleasantry: "How are you,",
name: "buddy",
exclamation: "?",
question: "some coffee?"
}
}
let name = "John"
let exclamation = "!?"
let question = "lunch?
function sayHello(, , name, question, exclamation)
=> "Good morning! How are you, John!? Do you want to grab lunch?"

When you’re working on a project, you can convince yourself that you will never forget what a method, function, or specific line of code does. While sometimes may be true, generally you’re going to forget what your code does and having to read back through it line by line to figure out what’s going on is awful. Just take the three extra seconds to add comments to your code and debugging, refactoring, or adding new features will be a breeze, especially if you step away from your code for a while before coming back to it.

//This code will greet a user upon loginfunction greeting() {
alert("Welcome!")
}

Sometimes you want to call a method but you don’t want to pass every single parameter. There’s a better way than trying to use null to fill in the blank! Use a spread operator with an array since arrays are sparse in nature and can have blank values without issue.

method(...['parameter1', , 'parameter3'])

Full-Stack Developer, Software Engineer, and UX/UI Aficionado

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store