42
\$\begingroup\$

As of ECMAScript 2015, JavaScript has 33 reserved keywords, such as break, const and new, as well as 10 future reserved keywords, such as let and await.

Your task is to chain together the largest number of consecutive1 distinct reserved keywords2 while writing functional JavaScript code3.

  1. Consecutive reserved keywords - reserved keywords that are separated only by whitespace and/or parentheses and/or curly braces.
  2. Reserved keywords - any reserved or future reserved keywords as of ECMAScript 2015. Reserved keywords from older standards are excluded, a full list of allowed keywords is given below.
  3. Functional code - your code should run (state your runtime, if necessary), eventually halt, and not throw any runtime errors.

If your code needs a specific runtime, the used reserved keywords must not be no-ops in the given environment.

List of reserved keywords

await
break
case
catch
class
const
continue
debugger
default
delete
do
else
enum
export
extends
finally
for
function
if
implements
import
in
instanceof
interface
let
new
package
private
protected
public
return
static
super
switch
this
throw
try
typeof
var
void
while
with
yield

Scoring & examples

Your score will be equal to the largest number of consecutive distinct reserved keywords.
In the case of equal scores, shortest source code in bytes wins. Commented sections and strings don't count towards reserved keywords, but count towards byte count.

// score: 3
if (true) 0; else throw function() {}
             ^------------------^
// score: 2
let foo = typeof typeof void typeof void 0;
                             ^---------^
// score: 0
/* typeof default debugger */

// score: 0, doesn't halt or debugger is no-op, depending on the environment
debugger;

Standard loopholes apply.

\$\endgroup\$
9
  • \$\begingroup\$ Sandbox discussion, for those who are interested. \$\endgroup\$
    – Etheryte
    Commented Apr 3, 2018 at 23:07
  • \$\begingroup\$ I feel like this might be more interesting if you did not allow parentheses between the keywords. \$\endgroup\$
    – Wheat Wizard
    Commented Apr 3, 2018 at 23:56
  • \$\begingroup\$ Is newline allowed? \$\endgroup\$
    – l4m2
    Commented Apr 4, 2018 at 2:54
  • \$\begingroup\$ @l4m2 Yes, it's whitespace. \$\endgroup\$
    – Etheryte
    Commented Apr 4, 2018 at 7:13
  • 1
    \$\begingroup\$ @l4m2 The same 43-point solution is possible without newlines as well so perhaps I simply don't understand the point you're trying to make? \$\endgroup\$
    – Etheryte
    Commented Apr 4, 2018 at 9:59

9 Answers 9

57
\$\begingroup\$

43 words, 603 bytes

Uhm, is this a loophole? I have no idea how the hell this is legal JS, but it works and it uses every single keyword.

new (class await
{
    break(){}
    case(){}
    catch(){}
    const(){}
    continue(){}
    debugger(){}
    default(){}
    delete(){}
    do(){}
    else(){}
    enum(){}
    export(){}
    extends(){}
    finally(){}
    for(){}
    function(){}
    if(){}
    implements(){}
    import(){}
    in(){}
    instanceof(){}
    interface(){}
    let(){}
    package(){}
    private(){}
    protected(){}
    public(){}
    return(){}
    static(){}
    super(){}
    switch(){}
    this(){}
    throw(){}
    try(){}
    typeof(){}
    var(){}
    void(){}
    while(){}
    with(){}
    yield(){}
})
\$\endgroup\$
9
  • 5
    \$\begingroup\$ Not that familiar with JS, but... are these actually keywords when used in this context, or just strings that have the same lexical structure as keywords and are using sugar to avoid quotes? \$\endgroup\$ Commented Apr 4, 2018 at 9:07
  • 2
    \$\begingroup\$ @Leushenko Probably the second (although I'm also not too familiar with JS at all), as they look like function names in the way they are used. Still, normally it shouldn't be possible to use these keywords as functions or class names, but Siguza still did it, while still complying to the challenge rules of only using parenthesis and brackets. A nice find to complete this challenge with all words I'd say, even though most of the other answers look more impressive for actually using the keywords AS keywords. Still +1 from me, it seems we have a winner. \$\endgroup\$ Commented Apr 4, 2018 at 9:19
  • 8
    \$\begingroup\$ @user202729 Why though? Why make it overly complexly looking when it can be done so simple? Code-golfing is usually doing something as short as possible, but I personally still prefer to keep readability and simplicity to some extend (I know, I know, this isn't a code-golfing challenge). Some examples: I prefer to use -1, i+1, i-1 above ~0, -~i, ~-i when I can, just because it's clearer to read for those who aren't too familiar with code-golfing and ~ yet. Although I will of course always use i+~j and i-~j to golf that single byte of i-j-1 and i+j+1. ;) \$\endgroup\$ Commented Apr 4, 2018 at 9:25
  • 2
    \$\begingroup\$ Yes, I could mash it further, and try to get the byte count down... a beginning like if(this)return void typeof function(){} else debugger isn't hard to come up with, but after that it gets ugly fast. And yes, this totally turns keywords into identifiers - but for some of them you have to in order to be able to use them (e.g. enum). I just took it to the extreme, but isn't that what code golfing is all about? :P \$\endgroup\$
    – Siguza
    Commented Apr 4, 2018 at 9:35
  • 2
    \$\begingroup\$ @Magic No, supplying the name of a class-method is simply a grammatical context where literal tokens are treated as property names, even if they are reserved words. This is basically the same, semantically, as doing obj["break"] = function (){} etc. \$\endgroup\$
    – apsillers
    Commented Apr 6, 2018 at 1:17
57
\$\begingroup\$

37 words

if (void this) {
  do {
    with (protected)
      for (public in private)
        typeof static instanceof new (function implements() {
          var let
          try {
            throw (class extends {} {})
          } catch (package) {
            yield
          } finally {
            debugger
          }
          return
        })()
    continue
    break
  } while (interface)
} else {
  switch (delete await) { default : 42 }
}

Keywords not used:

  • case requires :
  • const requires =
  • export requires strict mode
  • import requires strict mode
  • super requires constructor
\$\endgroup\$
6
  • 7
    \$\begingroup\$ I admit I laughed \$\endgroup\$
    – nicael
    Commented Apr 4, 2018 at 7:09
  • 52
    \$\begingroup\$ It looks like typical Java boilerplate code, e.g. to calculate 1 + 2. \$\endgroup\$ Commented Apr 4, 2018 at 8:14
  • 2
    \$\begingroup\$ @EricDuminil This is the real reason it's called _Java_​Script. \$\endgroup\$
    – wizzwizz4
    Commented Apr 4, 2018 at 8:23
  • 2
    \$\begingroup\$ This is just wonderful. \$\endgroup\$
    – Pedro A
    Commented Apr 4, 2018 at 10:09
  • \$\begingroup\$ @apsillers that's not a dumb thing to do at all. In fact it's preferable to using let or var. Remember that const and let have more reasonable scoping than var; the variable defined is scoped to the loop body, not the function, so if it stays constant within an iteration, then const is fine. \$\endgroup\$
    – Dave
    Commented Apr 6, 2018 at 23:08
35
\$\begingroup\$

43 words, 302 299 bytes

switch(void function(){for(const interface in public)do with(package)try{break}catch(private){if(typeof
this)throw yield static instanceof new class extends await{case(){return}super(){debugger}import(){}export(){}enum(){}}
else continue}finally{delete let}while(protected)var implements}){default:}
\$\endgroup\$
2
  • \$\begingroup\$ Theoretical lowerbound 277 (each two words separated by one byte) \$\endgroup\$
    – l4m2
    Commented Apr 4, 2018 at 10:13
  • \$\begingroup\$ Nice work, currently you're only missing enum. \$\endgroup\$
    – Etheryte
    Commented Apr 4, 2018 at 10:16
10
\$\begingroup\$

21 24 26 words, 185 bytes

+1 2 words thanks to Arnauld, and +1 to 12Me21!

void(function(){do{with({})if(typeof true in this)throw{}instanceof new(class extends{}{})
else return delete{}
try{break}finally{yield}
continue
}while(false)})()
switch({}){case{}:{}}

Assuming I understood the challenge, this scores 24 words. The words without parentheses, brackets, and whitespace:

void function do with if typeof true in this throw instanceof new class extends else return delete try break finally yield continue while false switch case

24 words, 177 bytes

Without "true" and "false", which are apparently not keywords according to the question.

void(function(){do{with({})if(typeof{}in this)throw{}instanceof new(class extends{}{})
else return{}
try{break}finally{yield}
continue
}while(delete{})})()
switch({}){case{}:{}}

Words:

void function do with if typeof in this throw instanceof new class extends else return try break finally yield continue while delete switch case
\$\endgroup\$
5
  • \$\begingroup\$ (I accidentally deleted my comment instead of editing it, the contents were: "you can use delete{} in place of true/false, think (and just return nothing") \$\endgroup\$
    – 12Me21
    Commented Apr 4, 2018 at 0:06
  • \$\begingroup\$ Ah, yes. I didn't pay much attention to that rule. \$\endgroup\$
    – Arnauld
    Commented Apr 4, 2018 at 0:09
  • \$\begingroup\$ you can put case ... : or default: at the end (since : isn't allowed between keywords) \$\endgroup\$
    – 12Me21
    Commented Apr 4, 2018 at 0:15
  • \$\begingroup\$ Ot you can end with for(let _;0;); for +2 words (but you can't use case anymore). \$\endgroup\$
    – Arnauld
    Commented Apr 4, 2018 at 0:38
  • \$\begingroup\$ switch({}){case{}:{}} => switch({}){case{}:;}? Not know if ; is sometimes necessary \$\endgroup\$
    – l4m2
    Commented Apr 4, 2018 at 2:47
7
\$\begingroup\$

38 39

class await {}
class g extends await {
 constructor() {
  super()
 }
}
switch ({}) {
 case function(){
  for (let in public)
  do with(package){
   try{break}catch(private){
   if(typeof this)
    throw static instanceof new (class extends await {})
   else{continue}}finally{debugger}
  }while(void protected)
  var implements
  return 
  yield delete interface
  const a=0
 }:
}

words from "super" to "const"

Golfed version:

class e{}class g extends e{constructor(){super()}}switch({}){case
function(){for(let in public)do with(package)try{break}catch(private){if(typeof
this)throw static instanceof new(class extends await{})
else{continue}}finally{debugger}while(void protected)
var implements
return 
yield delete interface
const a=0}:}
\$\endgroup\$
5
  • \$\begingroup\$ Is it now golfing time? \$\endgroup\$
    – l4m2
    Commented Apr 4, 2018 at 4:25
  • \$\begingroup\$ I'm getting Uncaught SyntaxError: Unexpected token delete when I try to run your answer in my Javascript console on Google Chrome, How can I test if your solution is valid? \$\endgroup\$
    – Ferrybig
    Commented Apr 6, 2018 at 17:51
  • \$\begingroup\$ @Ferrybig I use Firefox and it works fine. I can't install new version Chrome because of system \$\endgroup\$
    – l4m2
    Commented Apr 7, 2018 at 0:08
  • \$\begingroup\$ I get this error in Firefox SyntaxError: yield expression is only valid in generators \$\endgroup\$
    – TheChetan
    Commented Apr 8, 2018 at 6:28
  • \$\begingroup\$ I use ff 52.7.2 \$\endgroup\$
    – l4m2
    Commented Apr 8, 2018 at 6:51
4
\$\begingroup\$

21 words

(not sure about let and await)

var await=String, let=String;
switch (await) {
    case void typeof new await instanceof let in (function()
{do{try{return this
if((class extends{}{})){}else{break}}finally{(delete {})}}while(false)})():break;
}
\$\endgroup\$
2
  • \$\begingroup\$ , = and ; are not allowed as separators. \$\endgroup\$
    – DELETE_ME
    Commented Apr 4, 2018 at 9:24
  • 3
    \$\begingroup\$ They aren't being counted \$\endgroup\$
    – soktinpk
    Commented Apr 4, 2018 at 15:20
4
\$\begingroup\$

14 15 16 Words with no brackets or newline

!function(){if(0);else do throw yield new public in void typeof this instanceof class await extends function async(){}{};while(0)}

Thank Bergi for +1

\$\endgroup\$
4
  • \$\begingroup\$ Can you make it a generator function and throw in a yield? \$\endgroup\$
    – Bergi
    Commented Apr 5, 2018 at 17:49
  • \$\begingroup\$ I tried to figure out what this does, but my head started spinning. Could you post an explanation? \$\endgroup\$
    – Philipp
    Commented Apr 6, 2018 at 17:01
  • \$\begingroup\$ I'm getting Uncaught SyntaxError: Unexpected token new when I paste inside my javascript console \$\endgroup\$
    – Ferrybig
    Commented Apr 6, 2018 at 17:49
  • 1
    \$\begingroup\$ @Ferrybig, replace function with function*, so it will be recognized as a generator. \$\endgroup\$
    – guest-418
    Commented Apr 6, 2018 at 19:28
3
\$\begingroup\$

43 words, 300 bytes

with(this)try{let protected}catch(package){if(delete yield)for(const interface in typeof public)do{throw implements instanceof private}while(static)else debugger}finally{switch(void new class extends function(){return}{export(){var await}import(){break}super(){continue}enum(){}case(){}}){default:0}}

More readably:

with(this)
    try {
        let protected
    } catch(package){
        if(delete yield)
            for(const interface in typeof public)
                do {
                    throw implements instanceof private
                } while(static)
        else
            debugger
    } finally {
        switch(
          void new class extends function(){return} {
              export(){var await}
              import(){break}
              super(){continue}
              enum(){}
              case(){}
          }
        ){
            default:0
        }
    }

I had to use the "reserved word as method name" to deal with

  • case (since I had already used default with my swtich),
  • export and import (since module-mode is always strict, which disqualifies with)
  • super (since it must be followed by a property access or placed in a constructor function), and
  • enum (which can never be used at all, being a reserved word with no grammatically-valid use)
\$\endgroup\$
1
  • \$\begingroup\$ My 39 solution shows super can appear in the beginning without being name, but too expensive \$\endgroup\$
    – l4m2
    Commented Apr 5, 2018 at 18:03
2
\$\begingroup\$

28 Words without brackets, 234 Bytes

Putting identifier names as method definition names was too obvious (for me at least), so I was looking for the longest consecutive distinct sequence of reserved words and whitespace in a code snippet.

I hope dead code after a return doesn't count as a loophole, but the code is still runnable if the used identifiers are declared.

function*_(){implements:{
let private
var public
return yield typeof static
delete protected
throw new interface in package
break implements
debugger
void this instanceof class await extends function async(){}{}
do
continue
while(0)}}

This exploits the fact that some of the future reserved keywords are only considered invalid in ES5.1's strict mode (apparently because engines didn't bother to block all of ES3's future reserved words so there was too much code using them out there on the web).

Similarly, the async and await tokens introduced in ES8 are only considered keywords in strict mode.

\$\endgroup\$
5
  • \$\begingroup\$ Can break and continue be there? \$\endgroup\$
    – l4m2
    Commented Apr 5, 2018 at 16:46
  • \$\begingroup\$ When I call the function, I get Uncaught SyntaxError: Undefined label 'implements', does your answer require a specific runtime? \$\endgroup\$
    – Etheryte
    Commented Apr 5, 2018 at 16:49
  • \$\begingroup\$ @Nit Oops, I didn't actually call it, I only checked whether it's syntactically allowed (and then optimised prosaicism :D). \$\endgroup\$
    – Bergi
    Commented Apr 5, 2018 at 17:44
  • \$\begingroup\$ @l4m2 Oh, you're right, they can't but it's a runtime error so I didn't notice :-( Needed a bit of arbitrary reordering to work (and a block with the label). \$\endgroup\$
    – Bergi
    Commented Apr 5, 2018 at 17:45
  • \$\begingroup\$ @Bergi On Firefox it's syntax error on tio nodejs it's runtime \$\endgroup\$
    – l4m2
    Commented Apr 5, 2018 at 17:50

Not the answer you're looking for? Browse other questions tagged or ask your own question.