JavaScript — introduction method, syntax specification, variable, data type


1、 Introduction of JavaScript

//1. Mode one

  //Write your JS code here

//2. Mode 2

2、 JavaScript syntax specification

//1. JavaScript is insensitive to line breaks, indentation, and spaces.

    PS: semicolon should be added at the end of each statement. Although semicolon is not necessary, in order to compress the program in the future, if semicolon is not added, it will not run after compression.

//2. All the symbols are in English. Such as brackets, quotation marks, semicolons.

//3. JavaScript comments:

  Single line comment: // I am a comment
   Multiline comment:
              Multiline comment 1
              Multiline comment 2

3、 Variable

1. Syntax for declaring variables

//1. Declaration before definition
var name; //  There is no need to specify a type when declaring a variable. The variable name can accept any type
name= "egon";

//2. Declaration and definition
var age = 18; PS: JS is a weakly typed, dynamic language

2. Variable name naming conventions

//1. It consists of letters, numbers, underscores and $, but it can't start with a number, and it can't be a pure number

//2. Strictly case sensitive

//3. Cannot contain keywords and reserved words (the keywords to be used in the future upgrade version).
For example: abstract, Boolean, byte, char, class, const, debugger, double, enum, export, extensions, final, float, goto
//4. Recommended hump naming method: when there are more than one meaningful word to form a name, the first letter of the first word is lowercase, and the other words are capitalized

//5. Hungarian naming: it is based on the prefix of the first character of the data type word

 3. Let in ES6

Before ES6, JS had no block level scope. ES6 added the let command to declare variables (the declared variables belong to the block level scope). The {} of the process control statement is the block level scope. Its usage is similar to VaR, but the declared variables are only valid in the code block where the let command is located. For example, the counter of the for loop is very suitable for the let command.

for(let i=1;i<=5;i++){}

4. Constant

Const is added in ES6 to declare constants. Once declared, its value cannot be changed.

const PI = 3.1415926;
PI=3 //TypeError: "PI" is read-only

4、 Data type

JS is a dynamic language: variables can store numbers, strings, etc. Variables will automatically determine their own type according to the type of stored content.

1. Number

JavaScript doesn’t distinguish between integer and floating-point types. It has only one number type, number

var x = 3;
var y = 3.1;
var z = 13e5;  It means: 13 times (to the fifth power of 10)
var m = 13e-5;  It means: 13 times (to the - 5th power of 10)
var n = NaN; //  Typeof n result "number"


var num=1.3456
num.toFixed(2) // "1.35"

Convert string type to number

#String to numbber
ParseInt ("123") // returns 123

#The Nan attribute is a special value that represents a non numeric value. This property is used to indicate that a value is not a number.
ParseInt ("ABC") // returns Nan

#With automatic purification function; Only the number at the beginning of the string will be retained, and the Chinese characters will disappear automatically. For example:
Console.log (parseInt ("18 ha ha ha"))// eighteen

#Only go to the end of the Chinese, not to the beginning
Console.log (parseInt ("hahaha 18")// NaN

#Number to floating point in string
ParseInt ("123.456") // returns 123
Parsefloat ("123.456") // returns 123.456 

#Automatic decimal truncation function: rounding, not rounding
var a = parseInt("1.3") + parseInt("2.6"); //a=3
var a = parseFloat("1.3") + parseFloat("2.6"); //a=3.9

Convert numeric type to string

#Number to string type
var x=10;
var y='20';
var z=x+y; // z='1020'
typeof z; //String

#Number to string type
var m=123;
var n=String(m)

var a=123;
var b=a.toString()

2. String

var a = "Hello"
var b = "world;
var c = a + b; 
console.log(c);  //  Get HelloWorld

Common methods:

method explain
.length Return length
.trim() Remove blanks
.trimLeft() Remove the left margin
.trimRight() Remove the white space on the right
.charAt(n) Returns the nth character
.concat(value, …) Splicing, splicing string usually use “+” sign
.indexOf(substring, start) Subsequence position
.substring(from, to) Get subsequence according to index
.slice(start, end) section
.toLowerCase() a lowercase letter
.toUpperCase() Capitalization
.split(delimiter, limit) division





 The difference between slice and substring

String.slice (start, stop) and string.substring (start, stop)

The similarities between them are as follows
If start equals end, an empty string is returned
If the stop parameter is omitted, the end of the string is taken
If a parameter exceeds the length of a string, the parameter is replaced with the length of the string

The characteristics of substirng() are as follows
If start > stop, start and stop will be exchanged
If the parameter is negative or not a number, it will be replaced by 0

The characteristics of silce are as follows
If start > stop, the two will not be exchanged
If start is less than 0, the cut starts from the character (including the character at the position) of the first ABS (start) from the end of the string
If stop is less than 0, the cut ends at the ABS (stop) character from the end of the string (excluding the position character)

PS: template string is introduced in ES6. The template string is an enhanced version of the string, which is marked with a reverse quotation mark (‘). Its purpose is to

#1. Can be used as a normal string
var msg = `my name is ayca`

#2. It can also be used to define multiline strings
var info = `

#3. And you can embed variables in strings
var name = "ayca";
var age = 18;
var msg = `my name is ${name}, my age is ${age}`; Note: if you need to use back quotation marks in a template string, use a backslash before it.

3. Boolean value

Note: unlike python, true and false are lowercase

var a = true;
var b = false;

Any data type can be converted to boolean type: empty string, 0, null, undefined, Nan are all false.

#Boolean is a false data type

#Boolean values of other data types are true, for example

4. Null and undefined

Null means that the value is empty. It is usually used when a variable needs to be specified or cleared, such as name = null;
Undefined means that when a variable is declared but not initialized, the default value of the variable is undefined. Also, when the function does not have an explicit return value, it returns undefined.
Null means that the value of the variable is empty, while undefined means that only the variable has been declared, but no value has been assigned.