Variables

Declare a variable

To declare a variable, we use a keyword var.

var num

This will create a variable named num and its value is null.

To assign initial value, just do:

var num = 10

Then, the value of num is 10.

Multiple variables

To declare multiple variables at once in var declaration, just add a comma after the first variable.

var a = 10, b = 20

It looks pretier:

var x = 5,
    y = 9,
    z = 7

Naming

Name rules:

  • Contain only a-z A-Z 0-9 $ _

  • Do not start with digits

  • Do not match the keywords

  • Variable names is sensitive case

There is no rule about naming convention, but we suggest using both camel case and snake case.

Notes

You cannot use the variable in its declaration.

A variable cannot be declared more than one in a scope.

All undefined variables will give an error.

There is no global variable, see the global module to store data globally.

Last updated

Was this helpful?