官术网_书友最值得收藏!

Variables

Variables are a fundamental concept that you need to understand when working with any programming language. Variables are simply names that we use to associate with a data value. At a lower level, these variables are areas of space carved out in a computer's memory that store data.

You can think of a variable as a box that has a name and that contains some sort of data. When we initially create the variable it is empty until data is assigned. Basically, variables give us the ability to store and manipulate data. In the following figure, we create a variable called ssn. Initially this variable is empty but is then assigned a value of 450-63-3567. The data value assigned to a variable can be of various types including numbers, strings, Booleans, objects, and arrays:

In JavaScript, variables are declared with the var keyword. In general, the names that you assign to your variables are completely up to you. However, there are certain rules that you need to adhere to when creating a variable:

  • Variable names can contain both text and numbers, but should never start with a number. Always start your variable name with a letter or underscore.
  • Spaces are not permitted. If you want your variable name to include multiple words, use camel case notation. This convention requires using lower case for the first word, and capitalizing the first letter of subsequent words. For example: myVariableName .
  • Special characters such as percentage signs and ampersands are not allowed within variable names. Underscores, however, are permitted and can appear anywhere within the variable name.

Other than that, you are free to create variable names as you wish but you should try to assign variable names that describe the data that the variable will be assigned. It is also perfectly legal to declare multiple variables with the same var keyword as seen in the following code example:

var i, j, k; 

You can also combine variable declarations with data assignments, as seen in the following examples:

var i = 10;
var j = 20;
var k = 30;

You may have also noticed that each JavaScript statement ends with a semicolon. The semicolon indicates the end of a statement in JavaScript and should always be included. Missing semicolons are the cause of many JavaScript programming errors.

主站蜘蛛池模板: 翁牛特旗| 广东省| 行唐县| 同江市| 靖江市| 雷山县| 盱眙县| 嘉定区| 江陵县| 红原县| 大悟县| 株洲县| 曲阳县| 华容县| 芜湖市| 什邡市| 潮州市| 屏山县| 灌云县| 如东县| 乐安县| 达日县| 军事| 广昌县| 自治县| 昌都县| 宜黄县| 乐陵市| 太康县| 周口市| 吉安市| 右玉县| 杂多县| 康乐县| 永平县| 临清市| 岗巴县| 永平县| 莲花县| 科尔| 肇庆市|