Is there a way to get the return value of functions without 'evaluating' them?

twitter logo github logo ・1 min read

So imagine this code:

let x = alert("hi") // I know this returns undefined...

right? I know 'alert' returns 'undefined', but, when assigned, it still does something that affects the webpage, though, so, how can I use functions that would have 'consequences', without said 'consequence(s)', but just the return value?
Sorry if I made it confusing, let me know, and I can try to clear up confusion.


twitter logo DISCUSS (10)
markdown guide

Sometimes you can.

Say we have this function.

function show_hi() {
  const greet = 'hi';
  return greet;

You don't want to call alert but you want to work with that greet value. You can use function composition. You can create a function that works with the future value and combine it with the function that has the side effect that you want to delay.

A utility function like this one can help.

const compose = (gx, fx) => (...args) => gx(fx(...args));

Now you could do this.

function compose(gx, fx) {
  return (...args) => gx(fx(...args));

function show_hi() {
  const greet = 'hi';
  return greet;

function concat_world(str) {
  return str + ', world';

const hi_world = compose(concat_world, show_hi);


Using hi_world would still show hi but it is posible to work with the value of greet inside the concat_world function, and nothing ever truly happens until you call hi_world.


Thanks, but the goal is to do something like:

let x = alert(0)
x // undefined


let y = console.log(0)
y // undefined

without the alert menu showing, and nothing being logged to the console.
You understand better?


But why would you want to do that? I mean, in the end all you get is a variable with the value of undefined, so you could remove the middle function and just do let x = undefined

If you tell us more about the big picture of what you're trying to achieve, we might be able to help you better.


I think there is no way to do exactly what you want. You can get close to that but it will not be what you want.


Yes, sometimes and it's horrible.

function foo() {
   alert('does alert')

// Introspection of the sickest kind 🤢

Where regexReturnValue looks at the body of the function and returns the value of return alert('hard coded') as a string.

This won't work with vars.

There are more complex ways but not worth mentioning unless you really want to do this.

This type of code isn't really acceptable for production, but tests, knock yourself out. That's just my opinion.


Where did you get the regexReturnValue from? Is it built-in? If not, can you show me code, or what package / lib you got it from?


Il tell you because you are clearly doing something ambitious. But I am robbing you of the opportunity to learn.

Strings in JavaScript have a couple of methods for using regex. There is also a regex literal type, I would imagine you would probably use the string method match inside regexReturnValue, this function represents a custom function that you would have designed to test the function body and extract the value of the return, it is not built in.

Given your example I suppose the regex pattern might be

const regexPattern = /((?:return)(\s+)(.*))/gm;

The above pattern in English says, look at all the lines in this string, from start to end, if you find the word, return followed by one or mor spaces, please match any value after that. In theory the match will be the first match or null if not found.

// "alert('hello world');" or null

About match:

So here are all the ingredients you need to test a string and extract the results. But you have to do the leg work because I'm mean, on my phone and about to go to sleep.

Classic DEV Post from May 29 '19

Is generalization killing creativity in the software industry?

As software gets more and more integrated into our lives, the industrialization of its crafting process becomes inevitable. But the over-generalization of software engineering can be crushing the creative side of programming.

PDS OWNER CALIN (Calin Baenen) profile image
I am a 13 (as of Oct 30 of 2019) yr/o developer (I have been developing mini-projects for 4, years now, since I was 9), who makes projects in languages like: Java, HTML, Python 3, JS, CSS, and C#.