We hold data in variables. Variable is used as a container for our data. Think about holding a name.
我们将数据保存在变量中。 变量用作我们数据的容器。 考虑持有一个名字。
Code:
码:
var name="Poftut";
console.log(name);
var is shortcut for variable. It is not a must but makes code readble. We can ommit the var while writing code
var是变量的快捷方式。 它不是必须的,但可使代码可读。 我们可以在编写代码时省略var
name is the name of the variable when we need to access the data we will use name
name是变量的名称,当我们需要访问数据时,我们将使用name
= assigns “Poftut” to the name variable
=将“ Poftut”分配给名称变量
Output:
输出:
Poftut
种类 (Types)
As developing applications there will be a lot of different data to work. From numbers to logic true,false. We need to use some types to define our data.
在开发应用程序时,将有许多不同的数据需要处理。 从数字到逻辑,真,假。 我们需要使用一些类型来定义我们的数据。
Say we develop a calculator. We want to add numbers we can write a code like below.
假设我们开发了一个计算器。 我们想添加数字,我们可以编写如下代码。
num1=12;
num2=4;
console.log(num1+num2);
Number is a type for javascript and differently interpreted by Javascript engine. Javascript engine is the interpreter that exists in the browser to run Javascrip code.
Number是javascript的一种类型,由Javascript引擎解释不同。 Javascript引擎是浏览器中运行Javascrip代码的解释器。
Or expressing logic values like True, False is different from text values.
或表示逻辑值(如True,False)与文本值不同。
We will look details of this types next chapters.
我们将在下一章中详细介绍这种类型。
范围 (Scope)
Scope means a variable use able area. For example we can not use variables declared other than current file or we can not access a varibale defined outside of function
范围是指可变使用范围。 例如,我们不能使用声明为当前文件以外的变量,也不能访问在函数外部定义的变量
Local Variable is only accessable from its scope
局部变量只能从其范围访问
Global Variable is access able from anywhere
全局变量可以从任何地方访问
Code:
码:
myVar="Global";
function test(){myVar="Local";}
console.log(myVar);
Output:
输出:
Global
As we see a function creates its own scope and variables created in this local scope can not be accessed from global scope
如我们所见,函数创建了自己的作用域,并且无法从全局作用域访问在此局部作用域中创建的变量
变量名(Variable Names)
Variable names can use different characters but there is some limitations.
变量名可以使用不同的字符,但是有一些限制。
We can not use break as variable name because it is a keyword for Javascript and used by Javascript. It will create confussion and error to use break. Here is Javascript keywords.
我们不能使用break作为变量名,因为它是Javascript的关键字,并由Javascript使用。 使用break会造成混淆和错误。 这是Javascript关键字。
abstract boolean break byte case catch char class const continue debugger default delete do double | else enum export extends false final finally float for function goto if implements import in | instanceof int interface long native new null package private protected public return short static super | switch synchronized this throw throws transient true try typeof var void volatile while with |
抽象 布尔值 打破 字节 案件 抓住 烧焦 类 const 继续 调试器 默认 删除 做 双 | 其他 枚举 出口 延伸 假 最后 最后 浮动 对于 功能 去 如果 实施 进口 在 | 实例 整型 接口 长 本机 新 空值 包 私人的 受保护的 上市 返回 短 静态的 超 | 开关 已同步 这个 扔 抛出 短暂的 真正 尝试 类型 变种 虚空 易挥发的 而 与 |
Variable names can not start with numbers. Which means
变量名称不能以数字开头。 意思是
12name is not valid variable name
12name无效的变量名
name12 is valid variable name
name12是有效的变量名
Javascript is case-sensitive language which is true for variable names. Myvar is different from MYVAR
Javascript是区分大小写的语言,对于变量名来说是正确的。 Myvar与MYVAR不同
翻译自: https://www.poftut.com/javascript-variable-types-scope/