Saturday, October 25, 2025
HomeLanguagesJavascriptVue.js | v-else directive

Vue.js | v-else directive

The v-else directive is a Vue.js directive used to toggle the display CSS property of a element only when the if condition is not satisfied. First, we will create a div element with id as app and let’s apply the v-else directive to a element with data. Now we will create this data by initializing a Vue instance with the data attribute containing the value.

Syntax:

v-else

Parameters: This directive doesn’t accepts any parameters.
Example 1: This example uses VueJS to show an element with v-else using arithmetic conditions.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        VueJS | v-else directive
    </title>
  
    <!-- Load Vuejs -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;width: 600px;">
        <h1 style="color: green;">
            neveropen
        </h1>
        <b>
            VueJS | v-else directive
        </b>
    </div>
  
    <div id="canvas" style="border:1px solid #000000;
                            width: 600px;height: 200px;">
        <div id="app">
            <h2 v-if="a > 10">a is greater than 10</h2>
            <h2 v-else>a is smaller than or equal to 10</h2>
        </div>
    </div>
  
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                a: 9
            }
        })
    </script>
</body>
</html>


Output:

Example 2: This example uses VueJS to show an element with v-else using booleans.

HTML




<!DOCTYPE html>
<html>
  
<head>
    <title>
        VueJS | v-else directive
    </title>
  
    <!-- Load Vuejs -->
    <script src=
    </script>
</head>
  
<body>
    <div style="text-align: center;width: 600px;">
        <h1 style="color: green;">
            neveropen
        </h1>
        <b>
            VueJS | v-else directive
        </b>
    </div>
  
    <div id="canvas" style="border:1px solid #000000;
                            width: 600px;height: 200px;">
        <div id="app">
            <h2 v-if="data">if is executed</h2>
            <h2 v-else>else is executed</h2>
        </div>
    </div>
  
    <script>
        var app = new Vue({
            el: '#app',
            data: {
                data: false
            }
        })
    </script>
</body>
</html>


Output:

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32361 POSTS0 COMMENTS
Milvus
88 POSTS0 COMMENTS
Nango Kala
6728 POSTS0 COMMENTS
Nicole Veronica
11892 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11954 POSTS0 COMMENTS
Shaida Kate Naidoo
6852 POSTS0 COMMENTS
Ted Musemwa
7113 POSTS0 COMMENTS
Thapelo Manthata
6805 POSTS0 COMMENTS
Umr Jansen
6801 POSTS0 COMMENTS