Understanding the behavior of invoking higher order functions can be quite perplexing

I'm encountering an issue with invoking a function within another function. I can't figure out why I am unable to invoke a function using the following logic:

function func1(futurefunc){
  futurefunc();
}

function func2(){
  return 3+3;
}

func(func2);

The code above returns undefined when it should be returning 6. Strangely, when I use func(alert), the alert function is successfully invoked. I'm puzzled as to why one case works while the other doesn't.

However, if I modify the code like this:

function func1(futurefunc){
  return futurefunc();
}

Now the code correctly outputs the value of 6. Can anyone provide a clear explanation for this behavior? I want to make sure I'm not missing anything.

Answer №1

In the initial scenario, func1 merely calls futurefunc() without returning any value, resulting in the output of 'undefined'.

If you pass the argument as 'alert', it triggers the alert function and displays a dialog box.

In the alternative situation, func1 invokes futurefunc and returns the result, which outputs 6.

This explanation shouldn't pose any confusion to grasp.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Building a versatile dropdown menu with ReactJS and creating reusable components

I am currently working on building a dropdown menu following a tutorial, but I have encountered a roadblock. Instead of using the "props" keyword as shown by the instructor in the tutorial, I passed the props directly as arguments without using props dot. ...

In Typescript, define a class with a property that will serve as the name of another type's property

Trying to define a class property for another type: class TypeOne { public static readonly code: string = 'code'; } class TypeTwo { public [TypeOne.code]: TypeOne } Encountering an error message: The computed property name in the class pr ...

Establish an action using the specified payload

Currently, I am working on a ReactJS application that incorporates redux and redux-actions. I am trying to create a redux-action with the login details in the payload as shown below: { type: 'AUTH_REQUEST', payload: { login: 'Foo&apos ...

Using the jQuery before() method to manipulate form fields

Is it possible to utilize the jQuery before method to insert a form? An example scenario could be as shown below: <script> $(document).ready(function() { $("button").click(function() { $("button").before('<form><input type="text ...

Show InfoWindow on Google Maps by hovering, not clicking

I have a query about integrating Google Maps with a jquery plugin for map display. Everything is functioning correctly, from marker positioning to page reload (with new database queries based on updated map coordinates, etc.) The only issue I'm faci ...

Tips for establishing a connection to a proxy server and executing an http.request through the proxy using nodejs

I'm looking to establish a connection to a proxy in order to send http requests through it. For instance: proxy.connect(someip:someport,function(){ console.log('[PM]['+this._account+'] Logging in..'); var auth_re = /auth& ...

Issue: Router.use() is expecting a middleware function, but instead received an Object

After updating to the latest version of Express, I encountered some middleware changes and tried modifying my code based on various solutions I found, but nothing seems to work. Previously, everything was functioning correctly, but I can't recall the ...

Change Scenery by Using Arrow Keys

I am trying to increase the speed of my background when the up and arrow keys are pressed. Despite my attempts, it is not working as expected. The variable this.speed controls the speed of the background. To achieve this, I implemented an if statement t ...

Tips on implementing the onChange event in a custom text input for React Native applications

Recently, I started exploring React Native and attempted to create a custom text input with suggestions following a tutorial. However, I encountered an issue where I couldn't use onChange on my custom text input. I attempted to set up a state in App.j ...

When you click on a list of links, a stylish image gallery will appear using fancybox

Can anyone lend a hand with this? I would greatly appreciate any assistance CSS <a id="fancybox" href="javascript:;">Gallery 1</a> <br /> <a id="fancybox" href="javascript:;">Gallery 2</a> <br /> <a id="fancybox" hr ...

Trigger an event once a script is called in an external HTML file loaded through Ajax

My webpage features an Ajax Loaded Div, within which a Jquery plugin loads my Google Spreadsheet. I am struggling to add event listeners to detect when the spreadsheet is fully loaded due to the unpredictable loading time and the limitations of listening f ...

Creating a distinctive appearance for JavaScript's default dialogue box

Is there a way to enhance the design of my code that prompts the user for input using JavaScript's `prompt`? Currently, it appears too simplistic. Are there any CSS or alternative methods to improve its appearance? function textPrompt(){ var text = ...

What are the steps to store a firebase storage downloadURL in a firestore collection?

I'm facing an issue with saving my firebase storage image into a firestore collection variable. Initially, it was working correctly but suddenly stopped functioning, and now the image variable is returning null. Note: I am using the asia-south1 serve ...

Is it possible to dynamically add plotLines to the xAxis using datetime in HighCharts?

Hey there! I've been playing around with adding plotlines in Highcharts and I'm loving it. It's really easy to define a date time on the xAxis for a plotline, like this: xAxis: { plotLines: [{ color: '#dadada', ...

I am looking to combine two arrays, potentially containing identical data

I have a task to combine two arrays, one from an API and the other from a database: Array1 (Data from API): { '0': { userid: 'kdkds', name: 'Stephan, email: '<a href="/cdn-cgi/l/email-protection" class="__cf ...

Modifying the CSS attributes within an iframe by referencing the parent window's properties

I am facing a challenge involving running an iframe on two separate pages within the same website, each with distinct CSS properties. The dilemma arises from the limited space available on my index.php page, where I aim to confine text within the iframe to ...

What is the correct method for attaching event listeners to newly added elements following an AJAX request for HTML content? (Using jQuery or JavaScript)

I'm working on a project where new setting pages are loaded via AJAX. I'm trying to figure out the best way to bind listeners to elements in the new content. One idea I had is to create a function that checks the file path and applies appropriat ...

Accessing the window variable within a Jquery selector in Javascript

I'm encountering some issues while attempting to optimize code. The following lines execute without errors: Array.prototype.forEach.call( $('ZA1 .stat'), function( td ) {//ExcuteCode} Array.prototype.forEach.call( $('ZA2 .stat'), ...

Issue with Vue: After removing an item from a v-for list, there is a remnant

Can you help me solve this issue with my Vue component setup? I have a list of components, each consisting of a preview canvas, a name, and a delete button. These components are stored in Vuex. For example, the list could look like this: | [W] Item 1 ...

discord.js: Bot keeps sending duplicate embeds

I recently set up my discord bot to respond with a message when I enter a specific command, but I've run into an issue where it's sending the same embed twice. I've tried troubleshooting, but so far I haven't been able to pinpoint the c ...