Building JavaScript from Six Characters: A jsFuck Tutorial
This article explains how JavaScript’s type coercion rules can be exploited to construct any string or code using only the characters []()+! and demonstrates step‑by‑step how to build letters, functions, and even execute code, illustrating the principles behind the jsFuck obfuscation technique.
JavaScript’s flexible type conversion allows strings, numbers, booleans, and objects to be created from the minimal character set []()+! , which the article calls the "golden rules": ! yields a Boolean, + yields a Number, and []+[] yields a String.
By combining these rules, simple expressions such as ![]===false , +[]===0 , and []+[]==="" are shown, then extended to extract characters from built‑in strings like (![]+[])[+!![]]==="a" and to build words such as fill , filter , and find using array method names.
The article demonstrates how to retrieve additional letters via constructor functions, e.g., true["constructor"]+[]==="function Boolean() { [native code] }" , and how to assemble the full alphabet by converting numbers to different bases with (12)["toString"](16)==="c" .
It then shows how to obtain punctuation and HTML tags using string methods like "test"["bold"]() which returns <b>test</b> , and how to invoke the global escape function via []["fill"]["constructor"]("return escape(' ')")()==="%20" .
Finally, the article explains constructing and executing arbitrary code with the Function constructor, for example Function("alert('test')")(); , and concludes that using only []()+!. one can achieve Turing‑complete JavaScript, as demonstrated by the jsFuck project.
Beike Product & Technology
As Beike's official product and technology account, we are committed to building a platform for sharing Beike's product and technology insights, targeting internet/O2O developers and product professionals. We share high-quality original articles, tech salon events, and recruitment information weekly. Welcome to follow us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.