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

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.

主站蜘蛛池模板: 开原市| 出国| 靖宇县| 三门县| 扎囊县| 鄂尔多斯市| 永清县| 榆中县| 夏津县| 论坛| 鸡西市| 天柱县| 安吉县| 洛扎县| 正宁县| 阿拉善右旗| 夹江县| 迭部县| 科尔| 屏边| 太谷县| 包头市| 灵丘县| 玛曲县| 江口县| 衡阳县| 含山县| 区。| 桐柏县| 西峡县| 且末县| 江都市| 永仁县| 利辛县| 宜宾市| 娄烦县| 章丘市| 保山市| 新巴尔虎左旗| 永登县| 民和|