We all know that javascript can be implicitly named as a variable. However, you must note that the implicit name variable is always created as a global variable. Check the following code to force the declaration of variables in javascript. We recommend that you declare variables using var. Variables in javascript are declared using the var keyword (variable.
The Code is as follows:
Var school = "beijingyizhong"
You can also use the var keyword to give multiple values to the variable.
The Code is as follows:
Var school = "beijingyizhong", diqu = "beijing", age = 100;
In addition, unlike java, javascript can store different data types in the same variable. For example
The Code is as follows:
Var school = "beijing ";
Document. write (school );
School = 132134;
Document. write (school );
In addition, javascript can be used without declaring variables, for example:
The Code is as follows:
Var test1 = "I am teacher ";
Test2 = test1 + "PK yuan ";
Document. write (test2 );
When javascript encounters an undeclared variable, it will automatically create a global variable for it. It is good to declare the variable for good use.
In addition, the variable declaration should follow the following rules,
The first letter of a variable can be a uppercase/lowercase English letter, underscore (_), or dollar "$ ".
The remaining letters can be underscores (_), uppercase/lowercase letters, any number, or dollar "$ ".
Variable names cannot be reserved words or keywords
The following are some valid variable names.
The Code is as follows:
Var test;
Var _ beijing;
Var $ djas;
The following are invalid variable names.
The Code is as follows:
Var 4 abcd; // starts with a number.
Var asdf "dad; // single quotes are invalid characters
Var false; // false is the keyword.
The above is all the content in this article. It is a basic knowledge and I hope you will like it.