官术网_书友最值得收藏!

Syntax basics

The most basic thing you can do in pretty much any programming language is declare a variable. Unlike most other languages, JavaScript is a dynamically-typed language, which means when you declare a variable, its value can be of any type and can change during the course of its lifetime. However, in contrast, a strongly-typed language dictates that a variable defined as a string type must always be a string and must always have a value of a string. The strong typed feature is included in es6 which we are going to learn next. For now, to declare a variable in JavaScript, simply use the var keyword before your variable name:

var myVariable;    // declaring a variable with no value 
var myFirstName = "Jason";   
var myLastName = "Krol"; 
var myFullName = myFirstName + ' ' + myLastName;  
// => Jason Krol 

The preceding code snippet shows how we declare variables and define them with initial values alongside their declarations. The + operator is used for string concatenation.

Also, we use camel case for the variable names. It is not mandatory that you use camel case for variable naming, but it is more common in object-oriented languages to follow camel case as opposed to the underscore-based approach.

JavaScript won't complain if you forget to put a semicolon at the end of each statement. Instead, it will attempt to put the semicolons for you if there are proper statement terminations missing. This can lead to unexpected results. The rules of semicolon insertion are explained in this article at http://bclary.com/2004/11/07/#a-7.9.1.

Since, es6 has introduced two more keywords for variable declarations, namely let and const, has made JavaScript a lot more elegant. First, lets learn const by considering the following example:

const loopOver = [1,2,3];

The usage of const is same as var. Declaring a variable with const makes itself immutable and it cannot be used for reassigning new content in itself.

One more distinction about the const keyword that it does not mean something is constant but it emphasizes one time assignment.

Let's test it by adding the following line:

loopOver = [4,5,6];

It throws the following error:

Uncaught TypeError: Assignment to constant variable

Well, Why is it needed? The recommended practice for coder is to keep things simple which means using a single variable to represent a single value for a time. However, we discussed about the dynamicity of the variable earlier which has its own advantages, sometimes there is need to represent a data which is immutable itself. Like store some credentials of server configuration or the Node packages itself. The usage can vary but will be applied with a single rule of one time assignment.

To study the let keyword, we need to know about the scope of variables first which is covered in the following section.

主站蜘蛛池模板: 泰顺县| 疏附县| 铜川市| 巴南区| 海伦市| 富民县| 克山县| 凯里市| 环江| 保靖县| 淮安市| 西林县| 深州市| 湖北省| 满洲里市| 荣昌县| 珲春市| 鄂尔多斯市| 富顺县| 济源市| 广德县| 渝北区| 沁阳市| 丰城市| 定远县| 嫩江县| 油尖旺区| 东丽区| 景泰县| 惠水县| 辰溪县| 公安县| 兴和县| 崇文区| 建平县| 游戏| 武鸣县| 林西县| 英山县| 邢台市| 南充市|