List of personal best tips for golfing with Javascript.

·

1 min read

List of personal best tips for golfing with Javascript.

A few months ago I discovered the world of code-golfing and started to solve my first challenges. Here is a list of the main tricks I have experimented and learned in javascript during these last months. If you have some too, don't hesitate to share them in comments!

Loop

for(;;) console.log(1)
let n = 11
for(;n--;) console.log(n)
/**
 * 10
 * 9
 * 8
 * ...
 * 0
 */

Functions

function add(n){
  const foo = bar => add(n+bar)
  foo.valueOf = () => n
  return foo
}

add(2)(3)(1) // 2 + 3 + 1 = 6
const foo = bar => console.log('foo', bar)
c=foo
c('bar') // foo bar

Arrays

const sentence = 'Fayred is bad'
const words = phrase.split` ` // ['Fayred', 'is', 'bad']
phrase.join` ` // 'Fayred is bad'

Operations

const double = n => n << 1
double(43) // 86