Quine(s) in JavaScript

Search for a command to run...

No comments yet. Be the first to comment.
Transferring files between your local machine and a virtual machine (VM) is one of the most common tasks in tech. We often use scp because it comes natively installed and has similar utilization to ssh, but it is the clunkiest tool for the job. In th...

Alias Alchemy is a Deno-powered server that fetches aliases from a GitHub repository based on your query. Think of it as your instant alias spellbook, perfect for setting up new environments in seconds. ⚡ Usage: $ alias-alchemy.ra101.dev/?q=py,shell,...

Reader discretion is advised!A good Sus-Saturday to all. In this article, I drag and drown you with my paranoia about AI. Let the mass psychosis begin! What are AI hallucinations?AI hallucinations are incorrect or misleading results that AI models ge...

I recently joined a company, that follows squash-rebase flow here, and as my Git KT was going on, I figured I should build these commands before actually starting development, now that I have started development, these turned out to be quite handy! ...

Aim: To create an iterable blockchain, which could work with an extendable transaction class. Here the link GitHub repo! Blockchain in Brief: Blockchain is fundamentally a ledger maintaining records of transaction, This ledger is publicly distribute...

Quine) is a program that takes no input but outputs a copy of its own code. Unlike other languages writing a quine in JavaScript/NodeJS is quite easy.
Here we go!
function quine() { console.log(quine.toString()) }
The key here is that any function in JavaScript can be converted into a string and can be printed. Also, console.log is not the only option, alert can be used too. Although it will not work in a node terminal.
Above is a function that prints its source code but it is not a file that can be executed. So let's add a Call statement,
function quine() { console.log(quine.toString()+" quine();") } quine();
Note that we needed to add something extra in the log statement to achieve our goal. And ; was probably not needed.
Let's make it a little elegant, We know that JavaScript can make a function run as soon as it is defined, By using an IIFE (Immediately Invoked Function Expression).
( function quine() { console.log("( " + quine.toString() + " )()") } )()
Note that we manipulated the log statement as required.
For some more Quines in NodeJS: https://catonmat.net/quine-in-node
Now let's take Arrow-Operator and Format-Strings into this equation and It becomes almost eye-blindingly beautiful!
($=_=>`($=${$})()`)()
To Understand let's remove IIFE and extra parentheses in the format-string. And also, add some spacing.
$ = _ => `$=${$}`
So, the first $ is a variable that contains an arrow function.
_ is a random parameter for the arrow function which remains unused.
After the arrow, this is our format-string which can be divided into 2 parts, the String, "$=", and the Variable which is first $ itself.
Lastly, A Quine needs to be executable but it doesn't mean that programs resulting in errors can't be a Quine. Here is an example
throw 0
^
0
Link: https://github.com/nmrugg/quine
This program when executed as a .js file with the help of NodeJS outputs its own source code.
How this works is, NodeJS returns an error at the first line, the rest of the code is how the error looks like.
If you make your own JS Quine or you want to share Quine in other programming languages, then please do write in the comment section.