ArticleslgStudy

science

JSFuck

JSFuck is a science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand JSFuck rather than just read about it. In short: JSFuck is an esoteric subset of JavaScript, where code is written using only six characters: [, ], (, ), !, and +. The name is derived from Brainfuck, an esoteric programming language that also uses a minimalistic alphabet of only punctuation.

Key takeaways

  • JSFuck belongs to science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect JSFuck to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of JSFuck from memory before moving on to harder problems.

Reference excerpt

JSFuck is an esoteric subset of JavaScript, where code is written using only six characters: [, ], (, ), !, and +. The name is derived from Brainfuck, an esoteric programming language that also uses a minimalistic alphabet of only punctuation. Unlike Brainfuck, which requires its own compiler or interpreter, JSFuck is valid JavaScript code, meaning that JSFuck programs can be run in any web browser or engine that interprets JavaScript. JSFuck is able to recreate all JavaScript functionality using such a limited set of characters because JavaScript allows the evaluation of any expression as any type.

History In July 2009, Yosuke Hasegawa created a web application called jjencode which could encode arbitrary JavaScript into an obfuscated form utilizing only the 18 symbols []()!+,\"$.:;_{}~=. In January 2010, an informal competition was held in the "Obfuscation" forum of the sla.ckers.org web application security site to come up with a way to get the minimum number of characters required down to less than eight: []()!+,/. Contributors to the thread managed to eliminate the need for the , and / characters. As of March 2010, an online encoder called JS-NoAlnum was available which utilized only the final set of six characters. By the end of 2010, Hasegawa made a new encoder available named JSF*ck which also used only the minimum six characters. In 2012, Martin Kleppe created a "jsfuck" project on GitHub, and a JSFuck.com website with a web app using that implementation of the encoder. JSFuck can be used to bypass detection of malicious code submitted on websites, e.g. in cross-site scripting (XSS) attacks. Another potential use of JSFuck lies in code obfuscation. An optimized version of JSFuck has been used to encode jQuery, a JavaScript library, into a fully functional version written with just the six characters.

Encoding methods JSFuck code is extremely "verbose": In JavaScript, the code alert("Hello, World!"), which causes a pop-up window to open with the text "Hello, World!", is 22 characters long. In JSFuck, the same code has a length of 6448 characters. Certain single characters require far more than 1000 characters when expanded as JSFuck. This section offers an overview of how this expansion works.

Numbers The number 0 is created by +[], where [] is the empty array and + is the unary plus, used to convert the right side to a numeric value (zero here). The number 1 is formed as +!![] or +!+[], where the boolean value true (expressed as !![] or !+[] in JSFuck) is converted into the numeric value 1 by the prepended plus sign. The digits 2 to 9 are formed by summing true the appropriate number of times. E.g. in JavaScript true + true = 2 and true = !![] = !+[], hence 2 can be written as !![]+!![] or !+[]+!+[]. Other digits follow a similar pattern. Integers consisting of two or more digits are written, as a string, by concatenating 1-digit arrays with the plus operator. For example, the string "10" can be expressed in JavaScript as [1] + [0]. By replacing the digits with the respective JSFuck expansions, this yields [+!+[]]+[+[]]. To get a numeric value instead of a string, one would enclose the previous expression in parentheses or square brackets and prepend a plus, yielding 10 = +([+!+[]]+[+[]]).

Letters Some letters can be obtained in JSFuck by accessing single characters in the string representations of simple boolean or numeric values like "false", "true", "NaN", "undefined" with an indexer (a number in square brackets). Other tricks are needed to produce other letters – for example by casting the string 1e1000 into a number, which gives Infinity, which in turn makes the letter y accessible. The following is a list of primitive values used as building blocks to produce the most simple letters.

Example: Creating the letter "a" "a": Taken from the string "false". The second character of "false" is a, which can be accessed with:

"false"[1]. "false" can be made from false+[], i.e. the boolean constant false plus an empty array. (false+[])[1]: We write false as ![] (negation applied to an empty array). (![]+[])[1]: 1 is a number, we can write it as +true. (![]+[])[+true]: Since false is ![], true is !![]. (![]+[])[+!![]] – which evaluates to "a".

Other constructs The Function constructor can be used to trigger execution of JavaScript code contained in a string as if it were native JavaScript. So, for example, the statement alert() is equivalent to Function("alert()")(). The Function constructor can be retrieved in JSFuck by accessing the constructor property of a well known function, such as []["filter"] (Array.prototype.filter), so alert() can be executed with []["filter"]["constructor"]("alert()")(). In modern browsers functions with shorter names are available, such as []["flat"] (Array.prototype.flat) and []["at"] (Array.prototype.at).

Character table The characters with the shortest JSFuck expansions are listed below. Other UTF-8 characters can be expressed as well but will generate considerably longer code.

Security Lacking the distinct features of "usual" JavaScript, obfuscation techniques like JSFuck can assist malicious JavaScript code in bypassing intrusion prevention systems or content filters. For instance, the lack of alphanumeric characters in JSFuck and a flawed content filter allowed sellers to embed arbitrary JSFuck scripts in their eBay auction pages.

See also Brainfuck - an esoteric programming language created in 1993 by Urban Müller.

References

External links JSFuck - Write any JavaScript with 6 Characters: []()!+ – web application for encoding JavaScript to JSFuck JavaScript code of the aforementioned converter JScrewIt - Another tool to convert JavaScript to JSFuck, with environment-specific optimizations Esolang - The esoteric programming languages wiki

Worked examples

Example 1 — a first encounter with JSFuck

Start with the simplest possible case. Write down what JSFuck claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to JSFuck before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about JSFuck ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of JSFuck

In research
JSFuck appears in science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses JSFuck in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
JSFuck is common in secondary-school and first-year university syllabi. It links to neighbouring topics Esoteric programming languages, JavaScript, so understanding it makes those chapters shorter.
In everyday life
Look for JSFuck outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “JSFuck” →

How to study JSFuck in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what JSFuck means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain JSFuck out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is JSFuck in simple terms?

JSFuck is an esoteric subset of JavaScript, where code is written using only six characters: [, ], (, ), !, and +. The name is derived from Brainfuck, an esoteric programming language that also uses a minimalistic alphabet of only punctuation.

Why does JSFuck matter?

Because it connects several science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study JSFuck?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on JSFuck.

Tags

  • Esoteric programming languages
  • JavaScript

Keep exploring