# How to define variables in ionic

**URL:** <https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461>\
**Category:** ionic-v3\
**Created:** [July 9, 2017, 1:06am UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461 "2017-07-09T01:06:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![\_Ali94](https://avatars.discourse-cdn.com/v4/letter/_/8e7dd6/32.png) [@\_Ali94](https://forum.ionicframework.com/u/_Ali94)\
**Post date:** [July 9, 2017, 1:06am UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/1 "2017-07-09T01:06:16Z")

</div>

Hi every one

I want to know how can I use variables in ionic2.  
For example: in ionic 1 I was use var or scope and rootscope to define variable that can be used in several controllers, in ionic 2 I don’t know how can I define variables that can be use in all application pages or at least one page.

So can u give me example of using public variable and private variable ?

---

<div class="post-metadata">

**Author:** ![vinay\_Avanto](https://avatars.discourse-cdn.com/v4/letter/v/3d9bf3/32.png) [@vinay\_Avanto](https://forum.ionicframework.com/u/vinay_Avanto)\
**Post date:** [July 9, 2017, 2:48am UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/2 "2017-07-09T02:48:37Z")

</div>

data:any;  
if you want specific datatype then you can use.

data: number;  
data:string;

hope this will help.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [July 9, 2017, 2:50am UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/3 "2017-07-09T02:50:21Z")

</div>

Don’t create global variables. It’s an antipattern at this point.

Learn Typescript and Javascript ES6. This will help you a lot.

For local variables, use `let`. Don’t use `var`.

Never give a variable the type `any`.

For values that multiple pages need, use a provider, and have the pages inject the provider.

---

<div class="post-metadata">

**Author:** ![\_Ali94](https://avatars.discourse-cdn.com/v4/letter/_/8e7dd6/32.png) [@\_Ali94](https://forum.ionicframework.com/u/_Ali94)\
**Post date:** [July 9, 2017, 3:47am UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/4 "2017-07-09T03:47:05Z")

</div>

Yes I could define the variable but still I can’t change it if i’m open an other page

For exampl: I defined the variable inside “app” ts, but if i’m in home page and try to change the value of variable, its show me an error and i should defined again.

---

<div class="post-metadata">

**Author:** ![\_Ali94](https://avatars.discourse-cdn.com/v4/letter/_/8e7dd6/32.png) [@\_Ali94](https://forum.ionicframework.com/u/_Ali94)\
**Post date:** [July 9, 2017, 12:02pm UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/5 "2017-07-09T12:02:38Z")

</div>

> [@AaronSterling](#):
>
> For values that multiple pages need, use a provider, and have the pages inject the provider

Can u give me a code example for this point for 2 pages ?  
Because I searched a lot to understand and did such as u mentioned here but I couldn’t find good examples.

---

<div class="post-metadata">

**Author:** ![AaronSterling](https://avatars.discourse-cdn.com/v4/letter/a/51bf81/32.png) [@AaronSterling](https://forum.ionicframework.com/u/AaronSterling)\
**Post date:** [July 9, 2017, 12:03pm UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/6 "2017-07-09T12:03:43Z")

</div>

> **[Passing Data Between Pages in Ionic 2](https://www.joshmorony.com/passing-data-between-pages-in-ionic-2/)**
>
> When building an application that has more than a few pages, there’s a good chance that those pages won’t be working in isolation. You may want to send data from one page to the next, s…

---

<div class="post-metadata">

**Author:** ![\_Ali94](https://avatars.discourse-cdn.com/v4/letter/_/8e7dd6/32.png) [@\_Ali94](https://forum.ionicframework.com/u/_Ali94)\
**Post date:** [July 9, 2017, 8:52pm UTC](https://forum.ionicframework.com/t/how-to-define-variables-in-ionic/97461/7 "2017-07-09T20:52:29Z")

</div>

Thank u so much, finally my problem solved by using “provider” 🙂
