The Wayback Machine - https://web.archive.org/web/20240915122649/https://www.geeksforgeeks.org/how-to-check-a-function-is-defined-in-javascript/
Open In App

How to check a function is defined in JavaScript ?

Last Updated : 30 Apr, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

In this article, we will check whether a function is defined or not in JavaScript. The JavaScript typeof operator is used to check whether the function is defined or not.

JavaScript typeof Operator

The typeof operator is used to find the type of a JavaScript variable. This operator returns the type of a variable or an expression.

Syntax:

typeof var

Parameters:

It contains a single parameter var which is a JavaScript variable. 

Return value:

It returns the type of a variable or an expression: 

Examples to Check a Function is Defined or Not in JavaScript

Example 1: This example checks the type of the function. If it is a function then it is defined otherwise not defined by using typeof operator. 

JavaScript
let defined = 'Not defined';

if (typeof fun === 'function') {
    defined = "Defined";
}

console.log("Function " + defined);

Output
Function Not defined

Example 2: This example checks the type of the function, If it is a function then it is defined otherwise not defined by using typeof operator by creating a function. 

JavaScript
function isFunction(possibleFunction) {
    return typeof possibleFunction === 'function';
}

function fun() { }

let defined = 'Not defined';

if (isFunction(fun)) {
    defined = "Defined";
}

console.log("Function " + defined);

Output
Function Defined

Previous Article
Next Article

Similar Reads

JavaScript Program to Check if String Follows Order of Characters Defined by a Pattern or Not
In this JavaScript article, we will see the solution to the problem of checking if a string follows the order of characters defined by a pattern or not. We have given a string and pattern as the input and our task is to develop a program that will check whether the input string follows the same pattern according to the pattern given. Example: To so
4 min read
How the User-Defined Function differs from Built-in Function in PHP ?
In PHP, functions are blocks of reusable code that perform specific tasks. They enhance code readability, modularity, and maintainability by encapsulating logic into named units. PHP functions can be categorized into two main types: user-defined functions and built-in functions. User-Defined FunctionsUser-defined functions are functions created by
2 min read
JavaScript is showing reference error "Prompt is not defined"
In this article, we cover the reference error that is "Prompt is not defined". It's important to understand the tools you are using. I know, this can be very overwhelming in the beginning. but here, we will solve this reference error with not only one solution but also make more ways to done properly. window.prompt() instructs the browser to displa
2 min read
How to create a variable using a user-defined name in JavaScript ?
In this article, we will learn to create a variable using the user-defined name in JavaScript. Variables in JavaScript: Basically, it is a container that holds reusable data in JavaScript. The variable's value can be changed during program execution and should be declared before using it. Before ES6 declares a variable, we just use the var keyword
2 min read
JavaScript ReferenceError - variable is not defined
This JavaScript exception variable is not defined and occurs if there is a non-existent variable that is referenced somewhere. Message: ReferenceError: "x" is not defined Error Type: ReferenceError Cause of Error: There is a non-existent variable that is referenced somewhere in the script. That variable has to be declared or make sure the variable
1 min read
Explain the difference between undefined and not defined in JavaScript
In JavaScript, they both are related to memory space and there is a very simple difference between them. If the variable name which is being accessed doesn't exist in memory space then it would be not defined, and if exists in memory space but hasn't been assigned any value till now, then it would be undefined. undefined: It is a JavaScript keyword
3 min read
JavaScript for...in loop not working - Object property is not defined Error
A for...in loop is a special loop in JavaScript that enumerates all the keys (object property names) of an object. With each new iteration, a new property string is assigned to the loop variable. Error: Uncaught ReferenceError: Object property is not defined If we try to compare this loop variable with a non-string variable a ReferenceError is gene
3 min read
How to Fix "ReferenceError: __dirname is Not Defined in ES Module Scope" in Javascript
The JavaScript developers working with the Node.js often encounter the error "ReferenceError: __dirname is not defined in the ES module scope" when transitioning from the CommonJS modules to the ES modules. This error occurs because the __dirname and __filename globals which are available in the CommonJS are not defined in the ES module scope. Unde
4 min read
How to Fix "ReferenceError: document is not defined" in JavaScript?
The "ReferenceError: document is not defined" error in JavaScript is a common issue that occurs when trying to access the document object outside the browser environment such as in Node.js. This error can also occur if the script is executed before the HTML document is fully loaded. In this article, we'll explore the causes of this error and provid
2 min read
PHP | defined() function
The PHP defined() function is an inbuilt function in PHP which checks whether a constant is exists or not, in other words, defined or not. Syntax: bool defined($constant_name); Parameter: This function accepts a single parameter as mentioned above and described below. $constant_name: This is required parameter. It specifies the name of the constant
1 min read
Why error "$ is not defined" occurred in jQuery ?
One of the most common errors faced by jQuery developers is the '$ is not defined' error. At first, it may seem like a small error, but considering the fact that more than 70 percent of the website uses jQuery in some form or other, this may turn out to create a huge mess. Reason behind this error: This error basically arises, when the developer is
2 min read
How to detect which one of the defined font was used in a web page?
So here, we will be discussing some methods which will help you to get your desired font from any website ! 1.WhatFont : Extensions such as WhatFont make it easy to detect the font on webpage. You just need to install it and click on the element you want to inspect. The result is shown in a floating box, in the context of your choice. It is availab
2 min read
D3.js line.defined() method
The d3.line.defined() method lets you specify whether there is data defined for a given data point or not. If this method returns false, this means that data point exists, else true. Syntax: d3.line.defined(data_point); Parameters: data_point: data_point to be checked. Return Value: This method returns a boolean value. Example 1: In this example, w
2 min read
D3.js lineRadial.defined() method
The d3.lineRadial.defined() method lets you specify whether there is data defined for a given data point, angle, or not. If this method returns false, this means that data point exists, else true. Syntax: d3.lineRadial.defined(data_point); Parameters: data_point: data_point to be checked, it can be an angle, radius, etc. Return Value: This method r
2 min read
D3.js areaRadial.defined() Method
The areaRadial.defined() method in D3.js lets one specify whether data is defined for a given data point or not. The value is checked for each point using the boolean value or function specified as a parameter to this method. The checked value is kept if this method returns true, otherwise it is omitted. Syntax: areaRadial.defined( data_point ) Par
2 min read
D3.js area.defined() Method
The d3.area.defined() method lets you specify whether there is data defined for a given data point or not. If this method returns false, this means that data point exists, else true. Syntax: d3.area.defined(data_point) Parameters: This function accepts a single parameter as mentioned above and described below. data_point: This parameter is the data
2 min read
How to hide elements defined as variables in jQuery ?
In this article, we will learn how to hide elements defined as variables in jQuery. These can be done using two approaches. Approach 1: In this approach, we will first select the element that has to be hidden and then assign it to a variable. We will then call the hide() method on the variable. This method will hide the element from the page. Examp
2 min read
Uncaught ReferenceError: controller is not defined
Uncaught ReferenceError: the controller is not defined is a common error message that can occur in JavaScript. It usually occurs when you try to access a variable or function that has not been defined or is out of scope. An example where this error occurs: For example, the following code will result in an Uncaught ReferenceError: the controller is
1 min read
How to allow classes defined in a module that can be accessible outside of the module ?
The TypeScript scripts written by default are in the global scope which means that all the functions, methods, variables, etc. in one file are accessible in all other TypeScript files. This can lead to conflicts in variables, functions as programmers can edit the function/variable name or value without any realization. Therefore, the concept of mod
3 min read
Uncaught ReferenceError: Angular is not defined
In this article, we will see the "Uncaught ReferenceError: Angular is not defined", along with knowing a possible way to resolve it. The ReferenceError object depicts an error that arises when the variable is not initialized yet or simply, the variable doesn’t exist in the current scope & is referenced. This error might arise due to the angular
2 min read
How are actions defined in Redux ?
Redux is the state management library for JavaScript apps. It is used to manage the data globally in the app. In redux, there are three parts: Actions ReducerStoreActions: Actions are the JavaScript objects that have information. Actions provide information for the store. There is one more term that is used to create the action called Action creato
3 min read
How to Fix the "partial is not defined" Error in EJS ?
Encountering an error message like "partial is not defined" can be a stumbling block when you're working with EJS (Embedded JavaScript) templates. This error typically pops up when you're trying to use the `partial` function in EJS, but it's not being recognized. Don't worry, though it's a common hiccup for many developers, and we've got you covere
3 min read
How to specify a list of pre-defined options for input controls in HTML?
Specify a list of pre-defined options for input controls by using the HTML list Attribute. A pre-defined option for input controls using HTML5's <datalist> element, allowing users to select from a predefined list while typing in an input field. Syntax: <input list="datalist_id"/>Example 1: The example below shows, the HTML form with an
2 min read
How to Fix Error EJS 'partial is not defined'?
This article provides a detailed explanation of the ejs 'partial is not defined' error, detailing various approaches to resolve it, from using native EJS features to creating custom functions. The error 'partial is not defined' occurs in EJS when you use a partial function that hasn't been defined. EJS itself doesn't come with a built-in partial fu
3 min read
How to Allow Classes Defined in a Module to be Accessible Outside of a Module ?
In Node.js, the modular system allows you to organize your code into separate files, known as modules. This not only helps in managing and maintaining your code but also makes it reusable. One of the key features of modules is the ability to export classes, functions, or variables so they can be accessed from other files. This article will guide yo
5 min read
How To Fix The Error “window is not defined” in Next.Js?
The "window is not defined" error is a common issue that developers encounter when working with Next.js. This error occurs because Next.js runs both on the server and the client, and the window object is only available in the client-side JavaScript environment. Understanding why this happens and how to resolve it is important for building robust ap
4 min read
How to fix "Next.js: document is not defined"?
The "document is not defined" error in next js happens because the document is a client-side browser object, which isn't available during server-side rendering. In this article, we'll explain why this error occurs and how to fix it. Table of Content When does this error occur?Steps for Fixing Document not definedBest practices to avoid document is
5 min read
AngularJS ReferenceError: $http is not defined
The provides a control service named AJAX – $http, which serves the task of reading all the data that is available on the remote servers. The demand for the requirement of desired records gets met when the server makes the database call by using the browser. The data is mostly needed in JSON format. This is primarily because for transporting the da
3 min read
How to Check a Function is a Generator Function or not using JavaScript ?
Given an HTML document containing some JavaScript function and the task is to check whether the given function is generator function or not with the help of JavaScript. There are two examples to solve this problem which are discussed below: Example 1:In this example, we will use functionName.constructor.name property. It functionName.constructor.na
2 min read
How to check a variable is of function type using JavaScript ?
A function in JavaScript is the set of statements used to perform a specific task. A function can be either a named one or an anonymous one. The set of statements inside a function is executed when the function is invoked or called. A function can be assigned to a variable or passed to a method. var gfg = function(){/* A set of statements */}; Here
3 min read