Today I made a little quine in JavaScript – someone mentioned quines and I realized I had never written one, so I wrote one. A quine is a program that can output its own source code (without taking any input).
My first attempt was a bit bloated, but got the job done:
var program = "var f=function f(){return 'var f='+f+';f(f)'; };f(f)"
I can verify that it works by evaluating the program and comparing the evaluated output to the original program:
program === eval(program)
I stripped it down a bit to end up with something I’m happier with, by making the function non-anonymous so that I could refer to it without having to bind it to a variable. Once that was done, I could make it an IIFE rather than adding a separate statement to run the function:
var program = (function f(){return '('+f+')()'; })()