Learning the front end (24) ~ js Learning (II): variable

Literal: numeric and string

"Literal" constant i.e., fixed value, can not be changed.

There are three kinds of literals:

  • digital

  • String

  • Boolean literals

(1) literal value is very simple, write up on the line, do not need any symbols.

    alert (886); // 886 is digital, so no quotation marks.

(2) literal string is also very simple, but it must be in quotation marks. It can be words, sentences and so on. E.g:

    the console.log ( '886'); 

    the console.log ( 'Number One ages, never stop');

Tips: 100 is the number, "100" is the string.

(3) Example Boolean literal:

IF (to true) { 
    the console.log ( 'if true, go there code); 
}

to sum up

Literals can be used directly, but we generally do not use the literal directly .

If the direct use of literal words, too much trouble. For example, more places to use the same literal, not as a pre-defined variable to hold literal.

Variable easier for us to use, so the development is to preserve a literal by variable instead of using literal directly .

The concept of variables

Variables: the container is used to store data. We get the data by "variable name", or even modify the data.

Variables can also be used to save the literal.

Essence : variable in the program is requested in a memory used to store spatial data.

Statements and variable assignments

Declare variables (defined variables)

In the prior ES6 grammar , consistent use varkeyword to declare a variable . such as:

var name; // declare a variable named name

Added: After ES6 grammar and , consistent use  const, letkeyword to declare a variable .

Assignment of variables

name= 'hi'

Statements and variable assignments, for example as follows:

var a = 100; // ES5 Syntax 

const b = hello; // ES6 Syntax 
let b = world; // ES6 grammar

PS: In JavaScript, always use var to define variables (before ES6),

Variable initialization

Experienced programmers will write together and assignment statements:

    var a = 100; // declaration, and assigning 100 
    the console.log (A); 100 // Output

And declare a variable assignment, we call variable initialization .

Supplementary variable declaration and assignment

Modify the value of the variable

After a complex variable to be re-assigned, it will be covered by the original value, the variable value will be the last Fu prevail.

At the same time declare multiple variables

At the same time declare multiple variables, simply write a var, separated by commas between multiple variable names.

var name = 'jack', age = 27, number = 100;

Several variables declared in exceptional circumstances

Recommended to declare a variable, then the use. This may be an error. details as follows.

Written 1, the first statement, and then assign :( normal)

A var; 
A = 100; 
the console.log (A); // print the results: 100

2 wording, not a statement, the assignment only :( normal)

= 100 A; 
the console.log (A); // print the results: 100

3 writing, only a statement, not assignment :( Note that the print undefined)

A var; 
the console.log (A); // print the results: undefined

4 writing, no statement, no assignment will complain directly :()

console.log (a); // will complain

1 and 2, although the wording of writing are normal, but the two are different wording

Naming variables

Uppercase letters can be used, and case-sensitive . That is a and A are the two variables.

Let's sort out the naming of variables:

  • It can only consist of letters (AZ, az), numbers (0-9), the underscore (_), dollar sign ($) composition

  • You can not start with a number . In other words, it must begin with a letter (AZ, az), underscore (_) or dollar sign ($). Spaces are not allowed in variable names.

  • Instead of using "keyword" and "reserved words" JS language reserved as variable names.

  • We recommend naming a hump . For example, getElementById, matherAndFather, aaaOrBbbAndCcc

  • Variable names are case-sensitive (javascript is case-sensitive language).

  • Variable names can not be longer than 255 characters .

  • Chinese can be used as a variable name . But not recommended , because low.

Identifiers, keywords, reserved words

Identifier

Identifier: In JS and all that are by our independent naming of identifiers can be called.

 For example: variable names, function names, attribute names, parameter names are identifiers belong. Popular terms, the identifier is when we write code for them from the name.

Naming variables and command rule identifier is the same.

Similarly, the identifier can not use language keywords and reserved words reserved .

Keyword

Keywords: JS itself refers to the use of the word, can not then they act as variable , function names and other identifiers .

JS keywords as follows:

break、continue、case、default、

if、else、switch、for、in、do、while、

try、catch、finally、throw、

var、void、function、return、new、

this、typeof、instanceof、delete、with、

true、false、null、undefined

Reserved words

Reserved words: in fact reserved for "keyword" . Meaning that although it is not the key, but the future may be the key, the same can not be used when they act as variable names, function names and other identifiers.

JS reserved word as follows:

abstract、boolean、byte、char、class、const、

debugger、double、enum、export、extends、final、float、goto

implements、import、int、interface、long、native、package、

private、protected、public、short、static、super、synchronized、throws、

transient、volatile

 

Guess you like

Origin www.cnblogs.com/Vincent-yuan/p/12381137.html