How to create a variable

Hello,
I want to store a string value in a variable, and in all my pages i want to use that variable .
my goal is, when i later change the variable value, it should immediately take effect in all the pages. currently i am going to all the pages and manually editing the text .
Please let me know how can i do this. Thank you

Hey
If the users are going to use it in your code you will need to use sessions so that you can use it in site code and in page code.

First of all import this is your start page code.

import {session} from 'wix-storage';

Then you can set a session based variable like below.

session.setItem("key_of_string", "your value");

// or sample below

session.setItem("todaysSpecial", "A hairdryer for only 99€");

Then in all pages where you want to access this just go like below

In top of page use the same imort statement as above.

Then…

let stringValue = session.getItem("key_of_string");

// or sample below

let stringValue = session.getItem("todaysSpecial");
console.log(stringValue); // Will print out "A hairdryer for only 99€"

Then the variable stringValue will hold the same string as you created in your first page or in the Site Page Code. You can also store whole json objects this way but don’t go pass 64kb in size then Wix will complain.

You could also make a Data Collection to hold this inside but it is way more advanced coding this.

2 Likes