How to define default value of a variable containing null if it's null? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How to define default value of a variable containing null if it's null?

Hello everyone, I want to display another value of a variable containing promt if the user clicks ok or clicks anywhere else in the body(if it's HTML page). Here's the code var name = prompt("Enter your name"); var nameLength = name.value.length; if (nameLength == 0) { name = "Visitor"; } alert("Thanks for Watching " + name); But it's giving error that can't read property

30th Aug 2022, 11:38 AM
Parth
Parth - avatar
1 Answer
+ 2
The error is hinting that the property doesn't exist. Have you already considered something like this? let x = prompt() if (x) { console.log("x = ", x); } else { x = "default"; console.log("No input, x = ", x); }
30th Aug 2022, 12:01 PM
Lisa
Lisa - avatar