Skip to content Skip to sidebar Skip to footer

Javascript Functions , Return Undefined

Hello Everyone hope you all doing great , this is my code , function with name and callback taking the name to callback function to make return the name and console.log it if i f

Solution 1:

Your doSomething() function doesn't return anything, which means an assignment using it will be undefined. But, that's not really the problem here.

The underlying problem is that you seem to be mixing two different data processing patterns here: if you're writing purely synchronous code, then use returning functions (which immediately return some value). If you need asynchronous code, then use a callback (which will "eventually" do something). Mixing those two patterns is a recipe for problems and frustration:

Either:

  1. don't name your function a "callback", and have it return its processed value, or
  2. make the callback responsible for doing whatever it is you were going to do with val.

Case 1:

function doSomething(data, processor) {
  return processor(data);
}

function passThrough(v) { return v; }

varval = doSomething("test", passThrough);
// immediately use "val" here in for whatever thing you need to do.

Case 2:

functiondoSomething(data, callback) {
  // _eventually_ a callback happens - for instance, this// function pulls some data from a database, which is one// of those inherently asynchronous tasks. Let's fake that// with a timeout for demonstration purposes:setTimemout(() =>callback(data), 500);
}

functionhandleData(val) {
  // use "val" here in for whatever thing you need to do. Eventually.
}

doSomething("test", handleData);

And if you want to go with case 2, you really want to have a look at "Promises" and async/await in modern Javascript, which are highly improved approaches based on the idea of "calling back once there is something to call back about".

2021 edit: a third option since original writing this answer is to use the async/await pattern, which is syntactic sugar around Promises.

Case 3:

asyncfunctiondoSomething(input) {
  // we're still _eventually_ returning something,// but we're now exploiting `async` to wrap a promise,// which lets us write normal-looking code, even if what// we're really doing is returning a Promise object,// with the "await" keyword auto-unpacking that for us.return someModernAsyncAPI.getThing(input);
}

functionhandleData(val) {
  // ...
}

asyncfunctionrun() {
  const data = awaitdoSomething("test");
  handleData(data);
}

run();

Solution 2:

functiondoSomething(name,callback) {
callback(name);
}

functionfoo(n) {
   console.log(n);
   return n;
}

var val = doSomething("TEST",foo);

Take a look at above code. When you call doSomething, which internally executes foo it prints on the console because thats what console.log is for. However, after this statement it returns n as well which then is received in doSomething. But its not being returned. To put it simply, what you are mainly doing is

functiondoSomething(name,callback) {
    const returnValue = callback(name);
}

If you call the above method, it will return undefined. To make it return correct value, you have to call "return returnValue". Similary you have to say return callback(name)

Hope this helps.

Happy Learning

Solution 3:

Inorder to assign the returning value/object of a function(in this case doSomething, it should have a return statement. Else the function returns nothing, so when you assign that to val, it is still undefined.

So you modify your code like this:

functiondoSomething(name,callback) {
    returncallback(name);
}
functionfoo(n) {
    return n;
}
var val = doSomething("TEST",foo);
console.log(val);

Solution 4:

undefined is implicitly returned if you don't have a return in your function.

when you call var val = doSomething("TEST",foo), you are aligning the return value of doSomething to val, which is undefined.

functiondoSomething(name,callback) {
  returncallback(name);
}

functionfoo(n) {
  return n;
}

var val = doSomething("TEST",foo);
console.log(val);

Post a Comment for "Javascript Functions , Return Undefined"