In JavaScript if a variable is not initialised with any value, then it is set to undefined. We can set a default value if a value is undefined. This can be done using two ways.
Example 1: By using if checks (Brute force). In this method, we will manually check whether a value is not null or not undefined, if so then set it to some default value.
- Example:
<!DOCTYPE html>
<html lang=
"en"
>
Â
Â<head>
   Â
<meta charset=
"UTF-8"
>
   Â
<meta name=
"viewport"
       Â
content=
"width=device-width, initial-scale=1.0"
>
   Â
<meta http-equiv=
"X-UA-Compatible"
       Â
content=
"ie=edge"
>
   Â
<title>Demo</title>
</head>
Â
Â<body>
   Â
<h1 style=
"color: green;Â
           Â
text-align: center;"
>
       Â
neveropen
   Â
</h1>
   Â
<h3 style=
"text-align: center;"
>
       Â
Replace a value
if
null
or undefined
   Â
</h3>
   Â
<div style=
"margin-top: 50px;
               Â
text-align: center;"
>
       Â
<input type=
"text"
id=
"anyId"
>
       Â
<button type=
"submit"
               Â
onclick=
"defaultValue_ifcheck()"
>
       Â
Submit
       Â
</button>
   Â
</div>
Â
ÂÂ Â Â Â Â
ÂÂ Â Â Â
<script>
Â
ÂÂ Â Â Â Â Â Â Â
// By using if-checkÂ
       Â
function
defaultValue_ifcheck() {
           Â
var
oldValue = document.getElementById(
"anyId"
).value;
           Â
var
newValue;
           Â
if
(!oldValue) {
               Â
newValue =Â
               Â
"This is default value: Geeksforneveropen"
;
           Â
}
else
{
               Â
newValue = oldValue;
           Â
}
           Â
alert(newValue);
       Â
}
   Â
</script>
</body>
</html>Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
- Output:
Example 2: By using logical OR (||) operator. In this method, if the value is null or undefined, then it will simply replaced by default value set by user.
- Example:
<!DOCTYPE html>
<html lang=
"en"
>
Â
Â<head>
   Â
<meta charset=
"UTF-8"
>
   Â
<meta name=
"viewport"
         Â
content=
"width=device-width, initial-scale=1.0"
>
   Â
<meta http-equiv=
"X-UA-Compatible"
ÂÂ Â Â Â Â Â Â Â Â Â
content=
"ie=edge"
>
   Â
<title>Replace a value
if
null
or undefined</title>
</head>
Â
Â<body>
   Â
<h1 style=
"color: green;Â
              Â
text-align: center;"
>
       Â
neveropen
   Â
</h1>
   Â
<h3 style=
"text-align: center;"
>
       Â
Replace a value
if
null
or undefined
   Â
</h3>
   Â
<div style=
"margin-top: 50px;
               Â
text-align: center;"
>
       Â
<input type=
"text"
              Â
id=
"anyId"
>
       Â
<button type=
"submit"
ÂÂ Â Â Â Â Â Â Â Â Â Â Â Â Â Â Â
onclick=
"defaultValue_or()"
>
         Â
Submit
       Â
</button>
   Â
</div>
</body>
<script>
Â
ÂÂ Â Â Â
// By using Logical OR (||)
   Â
function
defaultValue_or() {
       Â
var
oldValue = document.getElementById(
"anyId"
)
       Â
.value;
       Â
var
newValue = oldValue ||
           Â
"This is default value: Geeksforneveropen"
;
       Â
alert(newValue);
   Â
}
</script>
Â
Â</html>
- Output:
Note: In Method 2, don’t use the following newValue = 10 || oldValue;. It will always set newValue to 10 because 10 will always return true.