Learning javascript variables has been several times, today’s system to sort out, mainly from the definition, declaration, data type several aspects of the start

Variable declaration

Var keyword is used in all declared variables of js

At first, I didn’t understand this sentence, why couldn’t I use let? Let must be initialized, so only var can be used

var a;         // Declare a variable a
var b,c,d;     // When multiple variables are declared at the same time, separate the variable names with commas

console.log(a);// Display the value of variable A in the console with undefined output
document.write(a); // Print the value of variable A on the web page, output as undefined
Copy the code

Declaring variables Note:

1, the same variable can be different fu word for many times, modify the data type of the variable, the data type of the modified variable depends on the situation /2, the declared variable should conform to the naming norms of the variable 3, the specific data type of the variable, determined by the variable assignment.

Js variable naming rules

1The variable name must start with a letter and the underscore "_" or "$". Other characters can be letters, underscores, dollar signs, or numbers.2, do not use Spaces or other punctuation marks in variable names, and cannot start with a number.3The length of the variable name cannot exceed255A character.4, variable names are case sensitive. (javascript is a case sensitive language)5Variable names must be on the same line6, cannot use reserved keywords in script language, reserved words,true,falsenullAs an identifier. So far JS has described a set of keywords that have a specific purpose, typically to control the start or end of a statement, or to perform a specific action, etc. Keywords are also language-reserved and cannot be used as identifiers.if for switch  case  break  continue  while  var  function
Copy the code

Definition of variables

Const, let,var, const

1, the const

Js variable defined by const is a constant. Cannot be modified, and must be initialized (giving const an initial value, not null),

Note: Const cannot change the value if it is changed. Browsers don’t fail, they just fail.

Const cannot be declared twice in the same function, or both will fail.

const a = 5;// Once 5 is defined, it cannot be changed. 5 cannot be empty

constA;// Error, must be initialized
Copy the code

2, var

Var is a common definition variable,

Var defines that the underpay variable will be output if there is no initial value, and no error will be reported





The variable defined by var is in function scope, which means that the variable can be called outside the method,

function test(){		
			if(true) {var a = "China";
			    console.log(a); //China
			}
			console.log(a); // China
		}
		test(); 
Copy the code

The execution result

Var can be omitted, but it is not recommended to use this method. If you omit var to define a variable directly, the variable will be promoted to a global variable, which will raise ReferenceError

3.let

Let is a standard let from ES6. The variable defined by let is block-level scope and is only valid within the number of lines in let. Variables cannot be used outside the function.

It is equivalent to local variables. You cannot use let definitions of the same variable name repeatedly in a function, otherwise an error will be reported

Let also does not have the same variable promotion as var

Let definition variables must be declared before they are used, otherwise an error will occur.



In this function. In this function it was used without using let declaration foo, foo was not found so referenceError was returned,

4. Direct assignment

A = a;Copy the code

This method is not standard, not recommended, you know this point is good.

Note: Direct assignment, do not declare variables, defined variables are global variables, can be used in the global scope.

4. Data types

Online on js data type is more, I write here mainly refer to js senior program design of data type is mainly divided into the original value and reference value, the original value cannot be changed, the original value of the reference value can change are: numeric: number (whatever number is a numeric, does not distinguish between integer and decimal) string: String (Everything enclosed in quotation marks is a string.) Boolean: Boolean (true, false) Undefined: undefined Reference value: Object type: object (special value: null) Reference data type bject Includes: Object, array, function, and data references can be changed

1, number

1. Number contains integers and floating point numbers. In the console, floating point numbers are automatically converted to integers

 var num = 2.00
  console.log(num) // Output: 2, automatically converted to an integer
Copy the code

2. The highest accuracy of floating point numbers is 17 bits

2. The string string

2. The string has the length attribute, which can obtain the length of the string 3. A string is immutable, so changing a string is destroying the original string and creating a new string, not changing the original string. 4. String conversion, written at the end of the article.

3. The Boolean Boolean value

1. Booleans have only true and false values. True does not necessarily equal 1 false does not necessarily equal 0 depending on the situation 2. Case sensitive, lowercase values true and false are Booleans. Uppercase false and true are just identifiers, not Boolean values. Boolean () transforms a value into a Boolean value

4.Undefined

The declared variable is Undefined, and the value of the variable is Undefined. No corresponding parameter is provided when the function is called. The parameter value is Undefined

5.null

Null indicates an empty reference

Null is usually used as a placeholder for the fact that null does not equal any value, including null itself. Null, when checked with the Typeof operator, is of type Object.

6. Object References the data type

It mainly includes object, array, function and date. I haven’t finished sorting them out for the moment. I will continue to update them after sorting them out

6.3 Addendum: Data type and symbol unique value, new in ES6

5. Data type detection and conversion

I wrote a more detailed article on data type casting and detection, you can look at javascript data type detection in javascript data type conversion