Get Function Name

JavaScriptJavaScriptBeginner
Practice Now

This tutorial is from open-source community. Access the source code

Introduction

In this lab, we will explore the world of JavaScript programming and dive into the concept of functions. Specifically, we will learn how to log the name of a function using the console.debug() method and the name property of the passed function. This lab will provide hands-on experience in working with functions and debugging tools in JavaScript.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL javascript(("`JavaScript`")) -.-> javascript/BasicConceptsGroup(["`Basic Concepts`"]) javascript(("`JavaScript`")) -.-> javascript/ToolsandEnvironmentGroup(["`Tools and Environment`"]) javascript/BasicConceptsGroup -.-> javascript/variables("`Variables`") javascript/BasicConceptsGroup -.-> javascript/data_types("`Data Types`") javascript/BasicConceptsGroup -.-> javascript/arith_ops("`Arithmetic Operators`") javascript/BasicConceptsGroup -.-> javascript/comp_ops("`Comparison Operators`") javascript/BasicConceptsGroup -.-> javascript/functions("`Functions`") javascript/ToolsandEnvironmentGroup -.-> javascript/debugging("`Debugging`") subgraph Lab Skills javascript/variables -.-> lab-28342{{"`Get Function Name`"}} javascript/data_types -.-> lab-28342{{"`Get Function Name`"}} javascript/arith_ops -.-> lab-28342{{"`Get Function Name`"}} javascript/comp_ops -.-> lab-28342{{"`Get Function Name`"}} javascript/functions -.-> lab-28342{{"`Get Function Name`"}} javascript/debugging -.-> lab-28342{{"`Get Function Name`"}} end

How to Get the Name of a Function in JavaScript

To get the name of a JavaScript function, follow these steps:

  1. Open the Terminal or SSH.
  2. Type node to start practicing coding.
  3. Use console.debug() and the name property of the passed function to log the function's name to the debug channel of the console.
  4. Return the given function fn.

Here's an example code snippet that demonstrates how to get the name of a function in JavaScript:

const functionName = (fn) => (console.debug(fn.name), fn);

let m = functionName(Math.max)(5, 6);
// The function name 'max' is logged in the debug channel of the console.
// m = 6

In this example, the functionName function logs the name of the passed function to the console's debug channel and returns the function itself. The name property of the function is used to get its name.

Summary

Congratulations! You have completed the Get Function Name lab. You can practice more labs in LabEx to improve your skills.

Other JavaScript Tutorials you may like