Make a default value for a variable

A simple example of making a default value for a variable.
<!DOCTYPE html>
<html>
<head>
   
</head>
<body>

<script>
    
    function setFuel(liters) {
        this.liters = liters || 40 // using || 40 helps us make a default value for this.liters if nothing was specified in the liters funcion argument
        
        return this.liters;
    }

console.log("Default liters number is = " + setFuel()); // we have specified no argument and the default 40 has been used by the function
    
</script>

</body>
</html>

Responses

0 Replies