Constants and Variables
Constants and variables are used to associate a name (yourName or welcomeMessage) with a value (the number 42 or the string "Hi!").
A constant has a set value that cannot be changed; the value of a variable can be changed.
The keyword var is used to declare a variable.
The following example declares a variable, "a", and assigns its value as "42". var a = 42
It is possible to change variable values over time:a = 88
// Now "a" has a value of 88.
Multiple variables can be declared on a single line, separated with commas.
Constants
Declare a constant using the let keyword.
This example declares a constant named "one" and assigns it a value of 1:let one = 1
Declare multiple constants on a single line and separate them with commas:let x = 0.0, y = 0.0, z = 0.0
The value of a constant can never be changed. Trying to assign a new value to a constant results in an error.
Constant and Variable Names
Constant and variable names can contain almost any character, including Unicode characters:
However, constant and variable names must have no blank spaces, mathematical symbols, arrows, private-use (or invalid) Unicode code points, or line and box-drawing characters. Numbers can appear anywhere within a name, except for at the beginning.
Constants or variables of a certain type can't be declared again with the same name, nor can they be altered to store values of differing types. Also, a constant cannot be made a variable, nor can a variable be made a constant.
read more,lesson1 the basics
No comments:
Post a Comment