Why it worked
The video leverages a surprising and powerful JavaScript trick that feels 'illegal' due to its ability to execute arbitrary code from user input. This element of surprise and the demonstration of a seemingly simple line of code having complex functionality likely drove engagement.
Summary
This video demonstrates a powerful JavaScript hack using `alert(eval(prompt()))`. It shows how a single line of code can take user input, treat it as executable JavaScript, and then display the result, highlighting its 'illegal' feeling power.
Structure
- 1Introduce the JavaScript hack
- 2Explain how `prompt` and `eval` work together
- 3Demonstrate creating a function at runtime
- 4Show executing simple math
- 5Conclude on the 'illegal' feeling power of the code
On-screen text
javascript hacks
alert(eval(prompt()));
THIS
THIS ONE LINE OF
JAVASCRIPT LITERALLY
FEELS ILLEGAL.
IT FIRST ASKS
THE USER FOR
TEXT
THEN EVAL
TREATS THAT
TEXT LIKE REAL
JAVASCRIPT CODE,
AND FINALLY ALERT
SHOWS WHATEVER
THE CODE RETURNS.
NOW WATCH WHAT THIS DOES.
IF THE USER TYPES
THIS,
function hi(){
return "You're a JS king 👑";
} hi();
JAVASCRIPT WILL ACTUALLY
CREATE
THIS FUNCTION AT
RUNTIME
AND INSTANTLY EXECUTE
IT.
AND IF THE USER
TYPES SOMETHING SIMPLE
LIKE
(50 * 5) / 2
TURNS
IT INTO ACTUAL
CODE,
RUNS THE MATH, AND
ALERT SHOWS THE
FINAL
RESULT.
ALL OF THIS FROM
A SINGLE LINE
OF JAVASCRIPT.
THAT'S WHY IT FEELS ILLEGAL.