One. General
JavaScript is often called the Web language. Since the earliest JavaScript is used to create web pages effects and form validation.
JavaScript development environment to build
Built environment, and the environment is nothing less than to run the code writing code environment.
Common text-based code editor as follows:
l Notepad
the Sublime
l Atom
l Brackets
l VScode
Common integrated development environment as follows:
l Dreamwaver
l WEBSTRA
Node.js installation process
Access https://nodejs.org/en/
After installation, you can view the currently installed version node by node -v command in the console inside
NPM introduced
NPM full name Node Package Manager, is supplied by the official Node.js package management and distribution tools.
- NPM installation instructions: typing in the command line
asl install -g cnpm --registry = https: //registry.npm.taobao.org
- Initialize it after installing
npm init: npm initialization settings
- NPM After installation, the installation of a common input module, the following instruction
npm install readline-sync: an input module mounted
two. JavaScript basic grammar
1. Notes:
Single-line comments // footnotes
Multi-line comments / *
*
*
*
*/
2. Statement
JS end with a semicolon, each statement on a separate line. Semicolon can not fill, not an error, but the code compression of time may be a problem, it is recommended to add a semicolon.
let i = 10;
console.log(i);//10
A pair of braces can be combined into a plurality of blocks of code statements which
{
let i = 10;
console.log(i);//10
}
Note: JS parsing layer by layer, if a layer of error, the contents of the back will not be resolved.
3. Identifier
Meaning: Represents the symbol of a certain entity. That is, from their own name, the name can be used as variable names, function names, object names and so on.
Mandatory requirements
1. may be numbers, letters, underscores, and dollar symbols, may not contain other special symbols
2. Do not start with a number
3. prohibit the use of JavaScript in the keywords and reserved words to name
4. strictly distinguish between sensitive
soft requirements
look text EENOW
4. The three methods named
- Hungarian notation is Microsoft following nomenclature Hungarian thought by programmers invention, which is characterized by the identifier name begins with one or more lower case letters representing the data type of the variable.
例如:a_array, o_object, i_userAge, b_isPassed
2. hump nomenclature
Hump nomenclature actually divided into two types, one is the big hump, the other is a small hump.
● large hump has been called Pascal nomenclature, that each - words are capitalized
For example: UserName
● difference between small and large hump that hump, the first letter of the first word of a small hump is lowercase, first letter of the word is the big back
write
For example: userName
3. serpentine nomenclature
This nomenclature is common in the Linux kernel, the characteristics C + + standard library, Boost and Ruby, Rust and other languages serpentine nomenclature is separated with an underscore between words such as: user_ name, my_ first_ name
5. Keywords and reserved words
Described in ECMA-262 - a set of keywords having particular use. These keywords can be used to indicate the start or end control statements, or to perform certain operations. As a rule, the keyword is reserved for the language itself, it can not be used as an identifier.
ECMA-262 also describes another group of words can not be reserved as an identifier. Although the reserved words in the language are not yet any specific purpose. But they are likely to be used as a key in the future.
JavaScript keywords and reserved words as follows:
abstract、await、 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、let、 long、 native、 new、 null、 package、 private、 protected、 public、 return、short、static、 super、 switch、 synchronized、 this、 throw、 throws、 transient、 true、 try、typeof、var、 volatile、 void、 while、 with、 yield
6. Data Type Description
In JavaScript, the data types can be divided into two categories as a whole in terms of: simple data types and complex data types
● simple data types
Simple data types There are six kinds:
string, symbol, number, boolean, undefined, null symbol type which is the basic data types in which the newly added ES6
● complex data types
Complex data types is only one kind of object
Including arrays in JavaScript, regular, with its object types are Types
View Data Types
In JavaScript, we can view the data of a data type by typeof operator
console. log( typeof 10);//number
console. log(typeof true);//booleanconsole.
log(typeof 'Hello');//string
console. log(typeof [1,2,3]);//object
7. Variables
Next we need to explore any programming language is very important in terms of a thing, variable. The so-called variable is used to refer to eleven values stored in the memory. Of course, before we use a variable, you also need to do first thing is to declare a variable.
Declare variables
JavaScript variables declared in a way there are three kinds: var, let, const. Var which is now not recommended, because there will be a variable lift and other issues. (Later we will discuss specific to this issue)
Const and let the difference is that a variable declared const if it is a simple data type, then that can no longer be changed. And let variables declared whether simple data types or complex data types, in the back can be changed. Examples are as follows:
const variable declaration
const name = 'Bill';name = 'Lucy';
//TypeError: Assignment to constant variable.
let declare variables
let name = 'Bill';
name = 'Lucy';
console.log(name);
Assignment and initialization of variables
let a = 3;
Multiple variables can also be a one-time initialization, write it on one line
let x = 3,y = 4,z = 5;
If you declare variables not given initial value, the default value is undefined
let a;
console.log(a);//undefined
Use var to declare variables
Front mentioned, there are three ways in JavaScript variables declared: var, let, const. Var which now no longer recommended for use. This is because the use var to declare variables will be accompanied by some problems. Of course, these questions are often regarded as some of the features of JavaScript, such as repeated statements and omissions in the statement.
Repeat statement
If you are using the var keyword to declare a variable, then it is allowed to repeat the statement. But this time ignores the statement. If you re-statement and with the assignment, the equivalent is reassigned
Repeat statement with no assignment, JS engine will automatically ignore the back of the variable declaration
was test = 3;
each test;
console. log(test);//3
If the re-statement with the assignment, it will be eleven coverage data
was test = 3;
was test = 5;
console. log(test);//5
Scope
1. The global scope
This is the JS engine - came in in the operating environment. In the global scope of variables declared in called global variables. It features variable global variables that can be accessed anywhere.
let a = 5; // this is a global variable
2. local scope
In JavaScript, a pair of braces can produce a local scope. Inside the local scope variables called local variables, since it is a local variable, then only in the local scope can have access to the inside, the outside is not accessible.
{
Leti = 10;
Console log (I); // 10.
The console.log (I);
// a ReferenceError: Not defined IS I
mention in passing that, in the variable var declared with braces not local variables, and It is a global variable. Actually, this is the first to use var to declare eleven question the legacy of variables.
Data Type Description
In JavaScript, the data types can be divided into two categories as a whole in terms of: simple data types and complex data types
● simple data types
Simple data types There are six kinds:
string, symbol, number, boolean, undefined, null symbol type which is the basic data types in which the newly added ES6
● complex data types
Complex data types is only one kind of object
Including arrays in JavaScript, regular, with its object types are Types
View Data Types
In JavaScript, we can view the data of a data type by typeof operator
console. log( typeof 10);//number
console. log(typeof true);//booleanconsole. log(typeof 'Hello');//stringconsole. log(typeof [1,2,3]);//object
Boolean
The so-called Boolean type, also known as boolean type, that is, true and false, the value of this type of only two - one is true, the other is false
let i = true;
console.log(i);//true
console. log(typeof i);//boolean
It should be noted that these two values are not the same numeric value, so true is not necessarily equal to 1 and false is not necessarily equal to zero. There is also a - point is the Boolean literals true and false are case sensitive. In other words, True and False are not a Boolean value.
Although the type Boolean literals only two, but in ECMAScript values of all types can be converted to a Boolean. You can use Boolean () function to convert other types Boolean value.
console.log(Boolean("Hello"));//true
console. log (Boolean(42));// true
console. log(Boolean(0));//false
Digital Type
- Integer
Binary begin with 0b, octal begin with 0, hexadecimal beginning with 0x
The results still to decimal
- Real
Floating-point representation of two ways: decimal and scientific notation type type.
let a = 3.14;
let b = 9.12e+2;
console.log(a,b);//3.14 912
- Numerical extension
JS minimum value supported console.log (Number.MIN_VALUE); // 5e-324
JS the maximum supported console.log (Number.MAX_VALUE); // 1.7976931348623157e + 308
- In:
English stands for Not a Number
Any operation involving NaN will return NaN
let A = + 10;
console.log(a);//NaN
console.log(isNaN(NaN));//true
console.log(isNaN("123"));//false
console.log(isNaN(123));//false
console.log(isNaN("Hello"));//true
console.log(isNaN(true));//false
NaN number belongs to type console.log (typeof NaN); // number
Numeric conversion
number () can be converted to non-numeric value
console.log(Number(true));//1
console.log(Number(false));//0
console.log(Number(10));//10
console.log(Number(null));//0
console.log(Number(undefined));//NaN
console.log(Number("1"));//1
console.log(Number("012"));//12
console.log(Number("0o10"));//8
console.log(Number("0b111"));//7
console.log(Number("3.14"));//3.14
console.log(Number("0xf"));//15
console.log(Number(""));//0
console.log(Number("123Hello"));//NaN
parseInt (): the value of non-integer values into
console.log(parseInt("1"));//1
console.log(parseInt("012"));//12
console.log(Number("0o10"));//8
console.log(Number("0b111"));//7
console.log(parseInt("3.14"));//3
console.log(parseInt("0xf"));//15
console.log(parseInt(""));//NaN
console.log(parseInt("123Hello"));//123
console.log(parseInt("012"));//12
console.log(parseInt("012",8));//10
console.log(parseInt("AF"));//NaN
console.log(parseInt("AF",16));//175
Static method
Number.isInteger (): used to determine whether a value of an integer.
console.log(Number.isInteger(25));//true
console.log(Number.isInteger(25.0));//true
console.log(Number.isInteger(25.1));//false
console.log(Number.isInteger("15"));//false
console.log(Number.isInteger(true));//false
Number.isFinite (): not a number when it returns false, it is, then returns true.
console.log(Number.isFinite(true));//false
console.log(Number.isFinite(7));//true
console.log(Number.isFinite(NaN));//false
console.log(Number.isFinite(Infinity));//false
console.log(Number.isFinite("23"));//false
Examples of methods
toFixed () String Return value rounded to the decimal representation specified
let num = 10.456;
console.log(num.toFixed(2));//10.46
console.log(num.toFixed());//10
console.log(num.toFixed(0));//10
console.log(num.toFixed(undefined));//10
console.log (num.toFixed (-1)); // given
String type
let a = "abcd";
let b = 'abcd';
let a = "Hello 'World',welcome";//正确
let b = 'Hello "World" , welcome'; // correct
let c = "Hello \"World\",welcome";//正确
Adding strings and other data types will be converted to a string
let a = "abcd";
let b = 13 + a;
let c = 3.14 + a;
let d = true + a;
let e = null + a;
let f = undefined + a;
console.log(typeof b);//string
console.log(typeof c);//string
console.log(typeof d);//string
console.log(typeof e);//string
console.log(typeof f);//string
toString () the addition null and undefined other than the data types into a string
let a = 10,b = true,c = null,d;
console.log(typeof a.toString());//string
console.log(typeof b.toString());//string
console.log (typeof c.toString ()); // given
console.log(typeof d.toString());
let i = 10;
console.log(i.toString());//10
console.log(i.toString(2));//1010
console.log(i.toString(8));//12
console.log(i.toString(10));//10
console.log(i.toString(16));//a
let a = 10,b = true,c = null,d;
console.log(String(a),typeof String(a));//10 string
console.log(String(b),typeof String(b));//true string
console.log(String(c),typeof String(c));//null string
console.log(String(d),typeof String(d));//undefined string
String template
Enhanced version of the string
let str = `Hello World`;
console.log(str);//Hello World
console.log(typeof str);//string
console.log(str.length);//11
let str = `Hello
World`;
console.log(str);
//Hello
//World
console.log(typeof str);//string
console.log(str.length);//12
let name = "xiejie";
console.log(`Hello,${name}`);
// Hello, xiejie
let count = 10,price = 0.25;
console.log(`${count} items cost $${(count*price).toFixed(2)}`);
//10 items cost $2.50