I have this basic code and it works fine when I type the name.
var user = prompt("Cual es tu nombre").toLowerCase();
if (user) {
console.log("Hola " + user);
} else if (user === null) {
console.log("Adios " + user);
}
The problem comes when I hit cancel. I get this error in the console:
Uncaught TypeError: Cannot read property 'toLowerCase' of null
Is there any way to print the content in case of giving cancel?
So it no longer generates an error
Hello, the problem is that when you cancel it, it continues to execute,
.toLowerCase()
a solution that I propose is to validate if the name variable has something defined and only execute the method if it has something assigned.