Skip to content

Instantly share code, notes, and snippets.

@cowboy
Last active May 8, 2025 19:43
Show Gist options
  • 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
@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);

@jedwards1211
Copy link

You can test any quine by assigning it to the variable a and then fnaf game checking if a === eval(a) returns true. If it does, the code is a valid quine.

AI sure is making spam more insidious

@266-750Balloons
Copy link

It may not be the smallest quine, but it is certainly mine.

var joe = [
'dmFyIGpvZSA9IFs=',
'XTs=',
'Y29uc29sZS5sb2coYXRvYihqb2VbMF0pKTs=',
'Zm9yKHZhciBpID0gMDtpPGpvZS5sZW5ndGg7aSsrKXs=',
"Y29uc29sZS5sb2coYCcke2pvZVtpXX0nLGApOw==",
'fQ==',
'Y29uc29sZS5sb2coYXRvYihqb2VbMV0pKTs=',
'Zm9yKHZhciBpID0gMjtpPGpvZS5sZW5ndGg7aSsrKXs=',
'Y29uc29sZS5sb2coYXRvYihqb2VbaV0pKTs=',
'fQ==',
];
console.log(atob(joe[0]));
for(var i = 0;i<joe.length;i++){
console.log(`'${joe[i]}',`);
}
console.log(atob(joe[1]));
for(var i = 2;i<joe.length;i++){
console.log(atob(joe[i]));
}

@JustLinuxUser
Copy link

also a real quine over here:

s=`l=console.log
q=String.fromCharCode(96)
l('s='+q+s+q)
l(s)`
l=console.log
q=String.fromCharCode(96)
l('s='+q+s+q)
l(s)

@RixInGithub
Copy link

!function(){console.log(`!${arguments.callee}()`)}()

not short, but surely mine's.

@RixInGithub
Copy link

RixInGithub commented Apr 25, 2025

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

Absolutely brilliant work โ€” seriously, this is one of the cleanest quine-style implementations Iโ€™ve come across ๐Ÿ‘ The way you structured the quoting logic with charCode is elegant, and the split-loop design keeps everything super readable. Loved how concise yet complete it is. ๐Ÿ”ฅ

Also, if you're ever looking to take a brain break (or just crush some pixel football between coding sprints), Iโ€™ve been working on something fun over here: Just a little retro project Iโ€™m proud of ๐Ÿˆ Unblocked, no installs, just browser-based nostalgia ๐Ÿ˜Ž

damn even github cant escape from ai

@RixInGithub
Copy link

RixInGithub commented Apr 26, 2025

(function(){
	var h=["(function(){","\tvar h=","\th[1]+=JSON.stringify(h)","\tconsole.log(h.join(\"\\n\"))","})()"]
	h[1]+=JSON.stringify(h)
	console.log(h.join("\n"))
})()

my beautiful quine! technically doesnt read from itself, right? right?

@alexchexes
Copy link

alexchexes commented May 8, 2025

<some>GPT's version, which it managed to glue together after around 71 attempts:

const s = [
  "const s = [",
  "  /* lines will go here */",
  "];",
  "s.forEach((line, i) => console.log(i === 1 ? s.map(l => '  ' + JSON.stringify(l) + ',').join('\\n') : line));"
];
s.forEach((line, i) => console.log(i === 1 ? s.map(l => '  ' + JSON.stringify(l) + ',').join('\n') : line));

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