ItSolutionStuff.com

How to Use Ternary Operator in Vue JS?

By Hardik Savani • July 10, 2023
Vue.JS

In this example, we will learn vue js ternary operator. i will show you how to use ternary ternary with v-model in vuejs. we can easily use ternary operator for condition in vue js.

You can easily apply ternary operator with v-model in vue js. you can see both example simple and using v-model too.

ternary condition is key of if condition. if you have small condition in your project than you never want to write if condition and long line of code beside that. But you will prefer ternary condition with single line and easy to use.

I will give you two example:

1) ternary condition for v-model

2) ternary condition for simple

You can see both example one by one, might be help you...

ternary condition for v-model

<!DOCTYPE html>

<html>

<head>

<title>How to use ternary operator in Vuejs? - ItSolutionStuff.com</title>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

</head>

<body>

<div id="app">

<input type="text" v-model="$data[myCondition ? 'name' : 'title']">

<div>Name: {{ name }}</div>

<div>Title: {{ title }}</div>

</div>

<script type="text/javascript">

var app = new Vue({

el: '#app',

data: {

name: '',

title: '',

myCondition:true

}

})

</script>

</body>

</html>

ternary condition for simple

<!DOCTYPE html>

<html>

<head>

<title>How to use ternary operator in Vuejs? - ItSolutionStuff.com</title>

<script src="https://cdn.jsdelivr.net/npm/vue"></script>

</head>

<body>

<div id="app">

<div>Name: {{ myVar == 1 ? 'Hardik' : 'ItSolutionStuff.com' }}</div>

</div>

<script type="text/javascript">

var app = new Vue({

el: '#app',

data: {

myVar:1

}

})

</script>

</body>

</html>

I hope it can help you...

Tags: Vue.JS
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Define Global Variables in Vue JS?

Read Now →

How to Get Data Attribute Value in Vue JS?

Read Now →

How to Get Query String Parameters in Vue JS?

Read Now →

How to Download File using Axios Vue JS?

Read Now →

Vue JS Get Dropdown Selected Value Example

Read Now →

How to Get String Length in Vue JS?

Read Now →

How to Convert String to Array in Vue JS?

Read Now →

Vue JS Get Array Length or Object Length Example

Read Now →

Vue JS Call a Function On Load Example

Read Now →

How to Get Current Date and Time in Vue JS?

Read Now →

Laravel Vue Datatables Component Example

Read Now →

Vue Axios Post Request Example Tutorial

Read Now →

How to Change Date Format using Moment in Vue JS?

Read Now →

How to Use setTimeout Function in Vue JS?

Read Now →

Laravel Vue JS Pagination Example with Demo

Read Now →