Code-golf techniques, part 1

Somewhat related photo by sydney Rae on Unsplash

Update: there are more things in part 2.

There is a game for programmers — code golf, and its particular implementation — The goal is to write a shortest possible program to perform some task, and that can be done in a variety of programming languages.

I have spent some time playing this game, and accumulated a number of techniques that help making programs short, and I want to share these techniques.


  • I’m not particularly good at code-golf, and I’m pretty sure there are much more advanced things that make your code shorter. And I’m not claiming this is a full list or anything like that.
  • I’m not going to provide ready solutions to any tasks. Not spoiling the fun! But there will be code snippets.
  • Discovering these techniques is fun on its own, so, maybe you should stop reading.
  • I’m going to talk mostly about JavaScript.
  • Some techniques are really obvious. Unless it’s four in the morning and you’re having your 17th cup of coffee. Real story here…
  • What is written below is not a good practice, not a clean code, not an elegant solution, nothing like that. The only criteria is: it saves characters? then it’s legit. I believe only some of these things can be used outside golfing.


  • Check if you really need exact value, e.g. task could mention “in range from 1 to 1000” but actual last that satisfies given conditions value could be something like 998.
  • Another thing would be using scientific notation:
// Long:
// Shorter:
  • Array of strings might be better formed by doing a split. If particular set of values allows that, use digit as delimiter to make expression shorter:
// Longest:
// Shorter:
'here goes some set of misc strings'.split(' ')
// Shortest:
  • Don’t use unicode sequences as actual characters are counted as 1 symbol, not as 2 or more UTF-8 bytes:
// Unusable:
// Long:
// Shortest:


  • Define by assigning initial value instead of let, const, var (and don’t forget they are global because of that):
// Long:
let a=1,b=2,c=3
// Short:
  • If using functions, check if you really need to pass variable; maybe having it global is just OK:
// Long:
// Short:
  • If you need variable local to function, define it as parameter, possibly with initialization:
// Example usage:
// Can't reuse global i, the following won't work with recursion:
// This one is safe, and only one character longer:
  • Use chained assignment if initial values are the same:
// Long:
// Short:
  • If you need to initialize array with some values, and some variables need to have same initial values, combine everything:
// Long:
// Short:
  • Destructuring assignment could be very useful:
// Swapping values:
// Computing next value and storing previous, all in one move:
  • Also, destructuring array into single variables might be shorter than using indexes:
// Long:
// Short:

That’s all for now. In the next story I’ll cover some expressions, type conversions, and more.