Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active December 23, 2023 08:51
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cowboy/6966747ad374fb97b0ab to your computer and use it in GitHub Desktop.
Save cowboy/6966747ad374fb97b0ab to your computer and use it in GitHub Desktop.
A few JavaScript quines
// Today, I noticed that someone favorited this tweet of mine from 2012:
// https://twitter.com/cowboy/status/232587756486938624
// !function $(){console.log('!'+$+'()')}() #quine
// So I fooled around for a little bit:
// Shortest JavaScript quine?
(function _(){return'('+_+')()'})()
// Have fun with this one!
(function $($_$,_,_$,_$_,$_){return[_=_[$_$](_$[$_++])][_[$_++]][_[$_++]]([][_[$_--]][_[$_++]](_$_,function($$){return _$[$$]||[$_,_$_,_,$_$,$][$_--]},$_++),_[++$_])})('split',',join,call,map',',()\'\\','1821383038303012434430383082',0)
// Test a quine like so:
a= your_quine_code
a===eval(a) // must be true
// Eg.
a= (function _(){return'('+_+')()'})()
a===eval(a) // true
@geoffswift
Copy link

Here's mine, it passes the test:
undefined

@jedwards1211
Copy link

@geoffswift lol come on, if that counts the shortest answer is just:

1

@jedwards1211
Copy link

jedwards1211 commented Dec 26, 2017

Here's one that works when saved to quine.js and run with node quine.js:

s = "; console.log('s =', JSON.stringify(s), s)" ; console.log('s =', JSON.stringify(s), s)

@phi548183
Copy link

okay here's my shortest quine:

yes. That's it.
...
okay, if you want a real one here it is:
function q(){return q+";q()"};q()
there.
magic.

@Radvylf
Copy link

Radvylf commented Aug 14, 2018

@phi548183 A quine cannot read its own source code

@Olsanking
Copy link

Olsanking commented Dec 10, 2018

Some idea.
Both are same in code

(_=$=>`(_=${_})()`)()
(l=i=>`(l=${l})()`)()

But not in text
image

@maximov-ru
Copy link

maximov-ru commented Apr 11, 2019

out by using console.log:
q="'";s='q="";s=;f=s.substr.bind(s);console.log(f(0,3)+q+f(3,4)+q+s+q+f(7));';f=s.substr.bind(s);console.log(f(0,3)+q+f(3,4)+q+s+q+f(7));

@Irfan434
Copy link

Irfan434 commented Jan 9, 2020

const f = () => {
  console.log("const f =", f.toString(), "; f();");
} ; f();

@adler3d
Copy link

adler3d commented Jun 30, 2020

(()=>{var arr=["(()=>{var arr=",0,";arr[1]=JSON.stringify(arr);return arr.join(\"\");})()"];arr[1]=JSON.stringify(arr);return arr.join("");})()

@gjstreicher
Copy link

a='r=(s,p,q)=>s.replaceAll(p,q);b="\'"+r(r(a,"\\\\","\\\\\\\\"),"\\\'","\\\\\'")+"\'";console.log("a="+b+";"+a);';r=(s,p,q)=>s.replaceAll(p,q);b="'"+r(r(a,"\\","\\\\"),"\'","\\'")+"'";console.log("a="+b+";"+a);

@cowboy
Copy link
Author

cowboy commented Sep 29, 2020

My current favorite is this because it's both the shortest and also has a lot of $$$ bling

($=()=>`($=${$})()`)()

[edit: not the shortest, see below]

@CodingInvoker
Copy link

CodingInvoker commented Dec 7, 2020

$=_=>`$=${$};$()`;$()

@cowboy
Copy link
Author

cowboy commented Dec 7, 2020

Nice! It seems like these two would be tied for shortest, but I think CodingInvoker's wins with a 50% higher bling-per-character ratio

($=_=>`($=${$})()`)()
$=_=>`$=${$};$()`;$()

@paperluigis
Copy link

ok
(function q(){return q;})

@JanRuebl
Copy link

Here`s mine:

@unknown81311
Copy link

unknown81311 commented Jan 16, 2021

Here`s mine:

can you explain that in greater detail?
I love the example but i am just curious how you got this.

@JanRuebl
Copy link

well, if you run it it prints out its own sourcecode. valid in every language.
a cheap trick within the quine rules.

@donno2048
Copy link

First of all, mine is:

(_=$=>`(_=${_})()`)()

But I prefer:

Uncaught SyntaxError: Unexpected identifier

@cowboy
Copy link
Author

cowboy commented Apr 21, 2021

Uncaught SyntaxError: Unexpected identifier

excellent

@SiddharthShyniben
Copy link

SiddharthShyniben commented May 8, 2021

Actually, these quines aren't real quines because they read themselves. Here's my take:

const charCode = 34;
const code = [
    "const charCode = 34;",
    "const code = [",
    "",
    "];",
    "for (let i = 0; i < 2; i++) console.log(code[i])",
    "for (let i = 0; i < code.length; i++) console.log(code[2] + '    ' + String.fromCharCode(charCode) + code[i] + String.fromCharCode(charCode) + ',');",
    "for (let i = 3; i < code.length; i++) console.log(code[i])"
];

for (let i = 0; i < 2; i++) console.log(code[i]);
for (let i = 0; i < code.length; i++) console.log(code[2] + '    ' + String.fromCharCode(charCode) + code[i] + String.fromCharCode(charCode) + ',');
for (let i = 3; i < code.length; i++) console.log(code[i])

repl

@SiddharthShyniben
Copy link

Of course it could be a lot shorter but I just wanted to show that we could add any amount of code and the quine would work with a few tweaks

@rubyswolf
Copy link

I made one that creates a string with code to log itself plus the parts around it then evaluates it
a="console.log('a='+String.fromCharCode(34)+a+String.fromCharCode(34)+';eval(a)')";eval(a)

@asararai
Copy link

asararai commented Dec 9, 2022

Does this count?
(() => { !function () { console.log((${arguments.callee.caller.toString()})()) }() })()

@rubyswolf
Copy link

I'VE DONE IT!
I created THE SMALLEST quine, it passes the quine test above and technically is one depending on how you define it.
It also should work in most programming languages!
You're never going to believe how amazing the code is:
0
Yep, that's it. Just the number 0. When run it prints 0, which is the code. It passes the test: 0===eval(0) making it the smallest quine (although not a very interesting one)

@donno2048
Copy link

@rubyswolf
It's not a Quine, someone already proposed this idea in this thread, and even if it does, the empty program results with an empty output so `` is the smallest Quine.

@rubyswolf
Copy link

oh I missed that one oops @donno2048

@zoqol
Copy link

zoqol commented Dec 18, 2022

without equality sign and function keyword

 ({0(){alert(`({${this[0]}})[0]()`)}})[0]()

@qtheperfect
Copy link

// Never ending eval...
let x ="let x =%22quinner%22;let y=decodeURI(x).replace(/quinner/, x);alert(y);eval(y);";let y=decodeURI(x).replace(/quinner/, x);alert(y);eval(y);

@qtheperfect
Copy link

// Never ending eval...
let x ="let x =%22quinner%22;let y=decodeURI(x).replace(/quinner/, x);alert(y);eval(y);";let y=decodeURI(x).replace(/quinner/, x);alert(y);eval(y);

@timothyferriss
Copy link

A quine in programming is a self-replicating code that, when executed, produces retro bowl its own source code as the output.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment