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

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.

主站蜘蛛池模板: 昆明市| 团风县| 五大连池市| 威海市| 乐平市| 惠州市| 大悟县| 封丘县| 万盛区| 钟祥市| 斗六市| 如皋市| 汪清县| 察隅县| 高要市| 兖州市| 北安市| 札达县| 奉化市| 镇原县| 泸溪县| 商都县| 油尖旺区| 确山县| 克拉玛依市| 正蓝旗| 临沂市| 岑溪市| 岫岩| 娱乐| 松江区| 龙海市| 枣阳市| 德昌县| 东乡| 青浦区| 河源市| 黄龙县| 花莲县| 福泉市| 赤峰市|