In JavaScript, variable names cannot have spaces, but variable values can have spaces. The variable name can contain numbers, letters, underscores and dollar signs, but Chinese characters and spaces cannot appear; and the variable value can have spaces, just define the variable value as a string type, for example "var a="hello world";" .
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
JavaScript variable names cannot have spaces, but variable values can have spaces.
JavaScript variable names
In JavaScript, variable names cannot be defined casually and need to follow the naming rules of identifiers, as shown below :
_, dollar signs
$;
double | goto | native | static | |
##class | ||||
interface | public | transient | const | |
long | short | volatile |
JavaScript variable value
After the variable is defined, you can use the equal sign = to assign a value to the variable, the equal sign The left side is the name of the variable, and the right side of the equal sign is the value to be assigned to the variable, as shown in the following example:
1 2 |
|
JavaScript is a dynamically typed language. When defining a variable, you do not need to specify the variable type in advance. The type of the variable is dynamically determined by the JavaScript engine during the running of the program. In addition, you can use the same variable to store different types of data, for example: <div class="code" style="position:relative; padding:0px; margin:0px;"><pre class='brush:php;toolbar:false;'>var a; // 此时 a 为 Undefined
a = "http://c.biancheng.net/"; // 此时 a 为 String 类型
a = 123; // 此时 a 为 Number 类型</pre><div class="contentsignin">Copy after login</div></div>
The data types in JavaScript can be divided into two types Type:
Basic data types (value types): String, Number, Boolean, Null, Undefined, Symbol ;
Reference data type: Object, Array, Function.
The string (String) type is a piece of text wrapped in single quotes '' or double quotes "" , such as '123', "abc". It should be noted that single quotes and double quotes are different ways of defining a string and are not part of the string. When defining a string, if the string contains quotes, you can use backslash
\to escape the quotes in the string, or choose different quotes from the string to define String, as shown in the following example:
1 2 3 |
|
[Related recommendations: javascript video tutorial
,
The above is the detailed content of Can there be spaces in JavaScript variables?. For more information, please follow other related articles on the PHP Chinese website!