Open In App

How to use if statements in Underscore.js templates ?

Underscore.js is a javascript library that provides helper functions in functional programming without extending the built-in objects. This package consists of 100 functions like map, filter, etc. 

We can install this by using npm.



npm install underscore

Syntax:

const _ = require('underscore');

Approach:



Example: This example shows the use of the above-explained approach.




<!DOCTYPE html>
<html>
<head>
    <script type="text/javascript"
                src=
    </script>
</head>
  
<body>
    <script type="text/javascript">
        <% if (typeof (number) != "undefined") {%>
            <span class="number"><%= number %></span>
            <%}
        else {
            print("Number doesn't exist");
        } %>
    </script>
</body>
</html>

Output:

Number doesn't exist
Article Tags :