".startsWith" giving errors on conditional Branches

I'm having some(many) problems when I use $gameVariables.value().startwith().

In a part of my game there's a terminal that allow the user to write everything. If he types nothing and press enter, the result is that:

https://i62.servimg.com/u/f62/15/27/47/75/sem_ty20.png
(The game doens't stop running nor show any problems, it just pop ups this error window that i expanded)


I tried to set the value as something so if the player types nothing there would be some value to that variable, but it seems to not solve the problem,. Also tried to add "else" to the conditional branch and leave it blank, but still gives error.Is there a way to fix that? Or at least to desactivate the error message, since it doesn't break anything in the game?
is it .startwith or .startWith or .startswith or .startsWith?

What you typed above is not what the error message indicates you used. And Javascript is case sensitive, so .startwith and .startWith are not the same things.

Are you sure the variable is being populated, and with a string? That function might not be available for a number. Do a Show Text \v[13] just before the conditional branch to make sure what's in there is what you think is in there.
is it .startwith or .startWith or .startswith or .startsWith?

What you typed above is not what the error message indicates you used. And Javascript is case sensitive, so .startwith and .startWith are not the same things.

Are you sure the variable is being populated, and with a string? That function might not be available for a number. Do a Show Text \v[13] just before the conditional branch to make sure what's in there is what you think is in there.
It's .startsWith, sorry.

Also i think it's possible that the problem is because the variable is not a string. I remember now that I had a code that changes that variable to int somewhere.Could you tell me how to change it back to string so i can test it?
Are you sure the variable is being populated, and with a string? That function might not be available for a number. Do a Show Text \v[13] just before the conditional branch to make sure what's in there is what you think is in there.
Pretty sure that is the issue. Type this into the console:
Code:
typeof($gameVariables.value(13));

Edit:
I remembered that $gameVariables.setValue("") is actually set to zero. Meaning, that setValue function does not like empty strings, so if it gets an empty string it will just set the value to zero.
Try checking the variable is not equal to zero before doing your startsWith check.

Edit2:
What I said in the first edit is not correct.
It's actually in the value() function. Since an empty string is a 'falsy' value, it gets returned as zero.
Code:
Game_Variables.prototype.value = function(variableId) {
return this._data[variableId] || 0;
};
The problem is probably that .startsWith() is a method applied to strings. When the value of the variable is null, it isn't a string, so .startsWith() isn't a valid function.

You should probably first cast the variable into type "string". You can do this by changing $gameVariables.value(13).startsWith("Secret") to String($gameVariables.value(13)).startsWith("Secret").
MV will let you set a variable to a string. Unless the player enters a number, it might change it to a number.

Is .startsWith() even a valid function for the version of JS used by MV?
.startsWith() is a built-in function in JS that is available on Strings.

I would suggest doing what @Llareian said.
Pretty sure that is the issue. Type this into the console:
Code:
typeof($gameVariables.value(13));

Edit:
I remembered that $gameVariables.setValue("") is actually set to zero. Meaning, that setValue function does not like empty strings, so if it gets an empty string it will just set the value to zero.
Try checking the variable is not equal to zero before doing your startsWith check.
It was indeed returning "number" when I tried that.
The problem is probably that .startsWith() is a method applied to strings. When the value of the variable is null, it isn't a string, so .startsWith() isn't a valid function.

You should probably first cast the variable into type "string". You can do this by changing $gameVariables.value(13).startsWith("Secret") to String($gameVariables.value(13)).startsWith("Secret").
.startsWith() is a built-in function in JS that is available on Strings.

I would suggest doing what @Llareian said.
Working now, thanks!
MV will let you set a variable to a string. Unless the player enters a number, it might change it to a number.

Is .startsWith() even a valid function for the version of JS used by MV?
Yes it seems to be, since it worked before if something was written



Thanks everyone for the help! I didn't think it would be so simple :S

Original thread: https://forums.rpgmakerweb.com/threads/startswith-giving-errors-on-conditional-branches.91339/