Tips and Tricks for Code Golfing in JavaScript
h
MrEconomical (2220)

Let's start off with the basics

What is code golf?

Code golf is a type of programming challenge in which the programmer must attempt to solve some challenges in the shortest number of characters possible. This may seem completely pointless and not applicable to the real world, and it is, but it's a fun exercise.

Tips and Tricks for Code Golfing

1. Write ungolfed code first - The most important thing is to have your program work. Character count should be a second priority. I recommend you write ungolfed, or normal, code first, and then try to make it shorter. A mistake I see many beginners make is they try immediately to start golfing their code and in the end they become frustrated why it throws a bunch of errors or gives the incorrect output. When code golfing, make sure it works first, then try to golf it.

2. Use ternary operators - Ternary operators are a shortcut for if else statements and are extremely useful. Not only can you shorten your existing if else statements, you can also assign variables and "return values" with them. Basically, how ternary works is condition ? execute_if_true : execute_if_false

// No ternary
if (a > b) {
    console.log("bigger")
} else {
    console.log("smaller")
}

// With ternary
console.log(a>b ? "bigger" : "smaller")

As you can see, in the first example I am not console.loging directly inside the ternary, but rather using it to "return" the correct result and then logging the result. The second example demonstrates how to set variables using ternary. One thing to note is that both statements to execute if true and if false must be present. This means you cannot do something like this: a>b?console.log("hi") and skip the else condition. One way to get around it is just like this: a>b?console.log("hi"):0. Of course, the spaces between the ternary operators can be removed, which leads me to my next point.

3. Remove your whitespace

JavaScript's whitespace is not part of syntax which means you can remove it all. Compress all of your code onto one line by taking advantage of semicolons. This is a simpler point but still incredibly useful.

console.log(1);console.log(2);console.log(3)

4. Use arrow functions

Arrow functions are useful both in normal programming and in code golf as they can shorten the code for anonymous functions. Instead of using function() {} for defining a function, you can simply use () => {} and pass parameters with param1 => {} or (param1, param2) => {}

setTimeout(() => {
    console.log("hi")
}, 500)

5. Consider built-in functions

Often, a built-in function can shorten your code immensely. Some useful built-in functions include forEach, map, reduce, sort, and many more. I especially recommend forEach for iterating through arrays:

array = [1, 2, 3, 4, 5]
array.forEach(a => console.log(a))

Closing

These are a few ways to improve your code golfing skills in the language of JavaScript. Code golf can be an incredibly fun challenge and is a good way to get more familiar with the programming language itself while teaching you new ways to think about problems.

TEST YOUR CODE GOLF SKILLS WITH ECOCODE: https://repl.it/talk/share/EcoCode-Competitive-Code-Golfing-Online/29877

You are viewing a single comment. View All
pyelias (1420)

@Coder100 array.map(console.log) works too