Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

How to use if statements in Underscore.js templates ?

  • Last Updated : 24 Aug, 2020

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.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

npm install underscore

Syntax:

const _ = require('underscore');

Approach:



  • Write your mark-up code regarding the requirement.

  • We should always place the underscore.js code inside the <script> tag.

  • In this case, Write the if and else logic based upon a problem statement.

  • Once the code is error-free we can surf the file as usual.

  • In this way, we need to write the code in <script> tag;

Example:




<!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:

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!