Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript toLowerCase() undefined error

Tags:

javascript

I know this has been put out too many times, but none of the questions fix the problem I have. It gives me the following error every time I run the function:

TypeError: Cannot read property 'toLowerCase' of undefined

Here's the code it's running:

global.toId = function(text) {
    return text.toLowerCase().replace(/[^a-z0-9]/g, '');
};
like image 739
zellman01 Avatar asked Oct 25 '25 08:10

zellman01


1 Answers

In ES2020, you can use optional chaining (?.)

global.toId = function(text) {
    return text?.toLowerCase().replace(/[^a-z0-9]/g, '');
};
like image 78
badrul Avatar answered Oct 27 '25 21:10

badrul