In JavaScript, you can use the var, const or let keywords to define variables, with the syntax "keyword variable name;" or "keyword variable name = value;". Variables defined by var can be modified; variables defined by const cannot be modified and must be initialized; let can define a local variable with a block-level scope.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Variables are one of the foundations of all programming languages. They can be used to store data, such as strings, numbers, Boolean values, arrays, etc., and set, update or read the contents of the variables when needed. We can think of a variable as a symbolic name for a value.
Any variable must be defined before being used. If multiple variables are defined, different storage spaces will be allocated for these variables.
How to define variables
You can use keywords: const, var, let to define variables in javascript, syntax:
关键字 变量名称;
Example:
var name; var name, age, sex;
Variable naming rules
In JavaScript, variable names cannot be defined casually and need to follow the naming rules of identifiers, as follows:
Variable names can contain numbers, letters, underscores _, and dollar signs $;
Chinese characters cannot appear in variable names;
Variable names cannot contain spaces;
Variable names cannot be keywords or reserved words in JavaScript;
The variable name cannot start with a number, that is, the first character cannot be a number.
When defining a variable, the variable name should be as meaningful as possible so that you or others can easily understand it. For example, you can use name to define a variable that stores names, and dataArr to define a variable that stores names. Variable of array type.
When the variable name contains multiple English words, it is recommended to use camel case naming (big camel case: the first letter of each word is capitalized, such as FileType, DataArr; small camel case: the first letter of the first word is lowercase and the following Capitalize the first letter of a word, such as fileType, dataArr).
The difference between const, var and let
Let’s take a look at the difference between const, var and let, the three ways to define variables in js.
1. Variables defined by const cannot be modified and must be initialized.
const b = 2;//正确 // const b;//错误,必须初始化 console.log('函数外const定义b:' + b);//有输出值 // b = 5; // console.log('函数外修改const定义b:' + b);//无法输出
2. Variables defined by var can be modified. If not initialized, undefined will be output and no error will be reported.
var a = 1; // var a;//不会报错 console.log('函数外var定义a:' + a);//可以输出a=1 function change(){ a = 4; console.log('函数内var定义a:' + a);//可以输出a=4 } change(); console.log('函数调用后var定义a为函数内部修改值:' + a);//可以输出a=4
3. let is a block-level scope. After the function is defined using let, it has no impact on the outside of the function.
let c = 3; console.log('函数外let定义c:' + c);//输出c=3 function change(){ let c = 6; console.log('函数内let定义c:' + c);//输出c=6 } change(); console.log('函数调用后let定义c不受函数内部定义影响:' + c);//输出c=3
[Recommended learning: javascript advanced tutorial]
The above is the detailed content of How to define variables in javascript. For more information, please follow other related articles on the PHP Chinese website!