Vue.js v-if with v-for Last Updated : 28 Apr, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report VueJS is a popular Javascript framework used in frontend development of various web applications. Some of its features include conditionally rendering an element, looping through all the elements to display the individual element, etc. The v-if and v-for both can be used on the same element, although, firstly, the v-if will be evaluated. Generally, it is not recommended to utilize the v-if and v-for with the same element, in order to avoid the implicit precedence. In this article, we will learn how to use v-if and v-for directives together to conditionally and dynamically render the elements in the VueJS. Approach We will create a Vue project, and then we will use 2 different approaches to use v-if alongside v-for. In the first approach, we will wrap v-for over a v-if to limit the elements to render on UI. In the second approach, we will create a computed property to pre-filter the items that will remain in the list. Example 1: In this example, we will use v-for as a wrapping tag over v-if. We will loop over numbers from 1 to 10 and only show the numbers on the page that are even. HTML <!-- App.vue --> <template> <p v-for="number in numbers"> <template v-if="number % 2 === 0"> {{ number }} </template> </p> </template> <script> export default { data() { return { numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } } } </script> Output: Example 2: In this example, we will create a computed property, filteredItems, to filter out the items in the list that are events. HTML <!-- App.vue --> <template> <p v-for="number in filteredItems"> <template v-if="number % 2 === 0"> {{ number }} </template> </p> </template> <script> export default { data() { return { numbers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] } }, computed: { filteredItems() { return this.numbers.filter(n => n % 2 === 0) } } } </script> Output Reference: https://vuejs.org/guide/essentials/conditional.html#v-if-with-v-for Comment More infoAdvertise with us Next Article How to Write Better Vue v-for Loops? D dishebhbhayana Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League Vue.JS Geeks Premier League 2023 +1 More Similar Reads Vue.js List Rendering v-for with v-if Vue.js is one of the best frameworks for JavaScript like React JS. The Vue JS is used to design the user interface layer, it is easy to pick up for any developer. It is compatible with other libraries and extensions as well. In order to repeat a task for a fixed amount of time, we make use of the f 3 min read How to Integrate Vite with Vue.js? Vite provides the flexibility and speed needed for modern web development by offering a fast-build tool and development server. Integrating Vite with Vue.js allows developers to use Viteâs efficient hot module replacement and optimized build processes while working with Vue's reactive framework. Thi 3 min read How to Loop X Times with v-for in VueJS? Vue.js is a progressive JavaScript framework widely used for building user interfaces. One of its core features is the v-for directive, which allows you to iterate over data or render a block of elements a specified number of times. we will explore various ways to loop X times using v-for in Vue.js. 3 min read v-for Directive in Vue.js v-for directive is a Vue.js directive used to loop over a data usually an array or object. First, we will create a div element with id as app and let's apply the v-for directive to an element with data. Now we will create this data by initializing a Vue instance with the data attribute containing th 1 min read How to Write Better Vue v-for Loops? Vue.js is a popular JavaScript framework used for building dynamic web applications. One of its key features is the v-for directive, which allows developers to render a list of items based on an array or an object. Writing efficient and clean v-for loops is crucial for optimizing performance and mai 2 min read Vue.js List Rendering v-for with an Object Vue.js is a progressive framework for building user interfaces. The core library is focused on the view layer only and is easy to pick up and integrate with other libraries. Vue is also perfectly capable of powering sophisticated Single-Page Applications in combination with modern tooling and suppor 3 min read Like