`substr` is deprecated

Hi,
Help me understand please ...

I have a function and variable var_push is a string value "06.10"; the function work fine...

img1

If I change the function (image)

img2

I have a alert? warning? (image)

img3

Why? Where is the problem?
(I change const to let but this is not the problem of course)

LMGTFY - Javascript Tips: slice, substr, substring | by Gustavo Alves | Medium.

1 Like

Entering let x = flow.get('var_push') means the editor has no idea what x is.
Lets say for arguments sake x is now an instance of MyLegacyString and MyLegacyStringhas a prototype function named substr - this is perfectly valid as far as the editor knows.

Entering let x = "06:10" makes the editor realise that x is a native string. And as @dceejay points out...

String.prototype.substr(…) is not strictly deprecated (as in “removed from the Web standards”), it is considered a legacy function and should be avoided when possible. It is not part of the core JavaScript language and may be removed in the future. If at all possible, use the substring() method instead

In short, use substring if you care - or use substr if you dont.

1 Like

There is no problem the editor is warning that substr is depreciated and would be best to use substring or slice. The warning is there so you know that substr is depreciated. The substr will still work as it is there for legacy code, so older scripts still work. You can move to substring or slice, as one day substr may stop working, if removed from JS as a legacy function/method. String.prototype.substr() - JavaScript | MDN for more info

1 Like

@dceejay
@Steve-Mcl
@E1cid

Thank you very much .....

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.