"State" is a way to keep track of data and settings in your skill.  For example, if you have a trivia skill and want to keep track of the player's score, you can store it in the skill's state.  If they get a trivia question right, you can add ten points to the player's score.  If they get the question wrong, you can subtract 2 points.   

State is specific to the current user and is persisted across sessions.  So, each user of your skill can have their own settings and you can keep track of their skill information separately.  Also, when users quit your skill and come back later, you can welcome them by name and even let them continue their game where they left off!  Pretty cool!

How to Set State

To store data and information in your skill, you use the "Set State" tool.  This tool is located on the Data tab of each of your intents.

Don't worry if the code in the above image seems a little scary at first, it's really simple and we'll teach you everything you need to know in a few short minutes.

For those programmers out there, you may recognize that code as javascript.  For everyone else, that's javascript.  Javascript is an easy-to-use language that we allow you to use to store and manipulate data in your skill.  For advanced users, you can do some really complex logic using this tool, but let's learn the basics first.

NOTE:  This is just a quick introduction to javascript.  To learn even more, just search for "learn javascript" in your favorite search engine.

What's a Variable?

A variable is simply a place to store some information that we want to use later.  We can use variables to store things like the player's name, their score, which question we are on, or the results of calculations.

Define a Variable

The most common thing you'll do with the Set State tool is create a variable and set it to some value.  You can do something this like:

let playerScore = 0;

The "let" keyword tells the system to create a new variable.  The name of the variable is "playerScore" and we've set it to an initial value of 0 .   Easy enough.

Variable Names

Here are the rules for naming variables:

  • Names can contain letters, digits, underscores, and dollar signs.
  • Names must begin with a letter
  • Names can also begin with $ and _ (but we don't recommend it)
  • Names are case sensitive (y and Y are different variables)
  • Reserved words (like JavaScript keywords) cannot be used as names

We recommend the following naming conventions:

  • Use camelCase when naming your variables
  • Start all variable names with a letter
  • Use descriptive names:  firstName instead of fn 

Assign a New Value to a Variable

If we want to update that variable later, we just write:

playerScore = 10;

This would update the existing variable named "playerScore" with a value of 10 .

Variable Math

We can add, subtract, multiply, and divide variables:

var a = 10;
var b = 20;
var c = 5;
var result = a + b - c;

The result would be 10 + 20 - 5 or 25 .

Just like in high school algebra, we can use parenthesis to force a different order of the operations:

var a = 10;
var b = 20;
var c = 5;
var result = a + (b / c);

In this case, the system would divide 20 by 5 first and then add the result to 10 .  result would, therefore, be 14 .

Arithmetic Operators

You can use the following operators in your calculations:

  • +  - Addition
  • -  - Subtraction
  • *  - Multiplication
  • /  - Division
  • %  - Modulus (Remainder)
  • ++  - Increment by 1
  • --  - Decrement by 1

Add or Subtract a value to or from a Variable

We can also add or subtract values to or from a variable.  To add 10 to the playerScore variable, we would write:

playerScore = playerScore + 10;

We can also use a shorthand version and write it like:

playerScore += 10;

The += operator here means "add 10 to playerScore ".

Similarly, to subtract 2 from the playerScore variable, we would write:

playerScore = playerScore - 2;

We could also write:

playerScore -= 2;

The -= operator here means "subtract 2 from playerScore ".

Assign Variables to other Variables

You don't have to use fixed values like 10 or 2 , you can also assign the value of other variables to a variable:

let a = 0;
let b = 10;
a = b + 2;

Here we are initializing a variable named a to 0 and a variable named b  to 10 .  Finally, we are changing the value of a to b + 2 .  This would set the value of a to 10 + 2 or 12 .

Assign Text to Variables 

Variables can also be set to text values instead of numeric values.  To do this, we surround the text we want to assign to the variable with quotation marks.

let opponentName = "Jerry";
let question = "What three colors appear on the flag of Ireland?";

Conditionally Assigning a Variable

Sometimes you want to set a variable to a value, but only if another variable has a certain value or meets a certain condition.  To do this, we use an if statement:

if (hour < 18) {
    greeting = "Good day";

Here we are setting the variable greeting to "Good Day" but only if the hour variable is less than 18 .


In addition to the < comparison operator above, you can use the following operators:

>  - greater than
<  - less than
 - equal to
!=  - not equal to
>=  - greater than or equal to
<=  - less than or equal to

Logical Operators

We can perform comparisons against multiple variables by using logical operators.  This comparison uses the && operator which means "AND":

if (level > 1 && isPremiumMember == false) {
  message = "You must be a Premium Member to access this level.";

So, this says "if the level variable is greater than 1 and the isPremiumMember variable equals false, then set the message variable to the text value".

Likewise, we can use the || operator to represent "OR":

if (color == "red" || color == "white" || color == "blue") {
  isUSFlagColor = true;

This says "if the color variable is either "red", "white", or "blue", then assign true to the variable isUSFlagColor


This has been a brief introduction to the javascript that you can use in the Set State tool of Voice Apps.  There are many resources on the internet that will show you even more examples of what's possible.  Feel free to reach out to us if you have any questions!


Did this answer your question?