Table of Contents
First, define the variable let (similar to var)" >First, define the variable let (similar to var)
Second, const declares a constant" > Second, const declares a constant
3. String expansion" >3. String expansion
Four, 5.0 template syntax:`Template string`" >Four, 5.0 template syntax:`Template string`
Five, 6.0 Arrow function: ( )=>{}" >Five, 6.0 Arrow function: ( )=>{}
Home Web Front-end JS Tutorial Detailed syntax of ES6

Detailed syntax of ES6

Sep 11, 2017 am 11:48 AM
Detailed explanation grammar

First, define the variable let (similar to var)

There has always been a bug in js which is var:

1. Variables declared by var will have variable promotion


console.log(name);  //jhonvar name = 'jhon';
Copy after login

2. Var does not have block-level scope


var name2 = 'jjjon';
{    var name2 = 'tom';
}
console.log(name2);     //tom
Copy after login

3. var can be used to define a variable multiple times, and the subsequent variable replaces the previous variable


var name3 = 'jond';var age = 18;var name3 = 19;var name3 = 'rose';
console.log(name3);     //rose
Copy after login

Newly defined variables let:

1. Variables declared by let will not be promoted. They can only be used later if they are defined in the front.


console.log(name4);     //报错let name4 = '1112';
Copy after login

2. let has block-level scope


let name5 = '222';
{
    let name5 = ' ttt';
}
console.log(name5);  //222
Copy after login

3. let cannot redefine a variable multiple times


let name6 = 'aa';
let name6 = 'bb';    //报错console.log(name6);  //aa
Copy after login

Second, const declares a constant

Constant: refers to data that will not change

1. The value cannot be changed


const pi = 3.01415;//pi = 3;     //报错
    {
        const arr = [5,6,8,9,];
        arr.push(7);
        console.log(arr);  //(5) [5, 6, 8, 9, 7]
        arr = 10;     //值不能改变,否则报错
    }
Copy after login

2. Constants have block-level scope


{
    const ban = "vin";
}
console.log(ban);     //报错
Copy after login

3. There is no variable promotion, declare first and then use


console.log(ba);     //报错const ba = 'liu';
Copy after login

4. Constants with the same name cannot be declared

5. An initial value must be assigned, otherwise an error will be reported


const bb;     //报错
Copy after login

6. If an object is declared, the address of the object cannot be changed, but its internal attributes can be changed


const obj = {
    na:"jjjj",
    age:20};
console.log(obj.na);  //jjjjobj.na = "ccs";
console.log(obj.na);  //ccs
Copy after login

For example: application scenarios, fixed addresses can use constants


// var path = 1122// var path = '1243';const path = 'path';
console.log(path);  //path
Copy after login

3. String expansion

1. Determine whether the string "hello word" exists "word"


var str = 'hello word';var result = str.indexOf('word');
console.log(result);  //6
Copy after login

2. ES6 provides includes(): returns a Boolean value, used to determine whether the string contains certain characters


var bool = str.includes('word');
console.log(bool);    //true
Copy after login

3, startsWith(str[,num]): Returns a Boolean value, used to determine whether the string starts with a certain character


 bool2 = str.startsWith('hello'
Copy after login

//Pass in 2 parameters to this method
var bool3 = str.startsWith('word',6);
console.log(bool3); //true

4, endsWith(str[,num]): Returns a Boolean value, used to determine whether the string ends with certain characters


var bool4 = str.endsWith('d');
console.log(bool4);  //true//给这个方法传入两个参数var bool5 = str.endsWith('o',7);
console.log(bool5);  //false
Copy after login

5, repeat(num): Pass in a number, this method will repeat the string the number of times corresponding to the number


var str322= '我爱我家,\n';
console.log(str322.repeat(3));        //3行 我爱我家,
Copy after login

Four, 5.0 template syntax:`Template string`


var obj33 = {
    name:'zhuzhu',
    age:18,
    sex:'男',
    hobby:'女',
    veight:120,
    height:180};// 字符串拼接方法var str4 = '大家好,我叫:'+obj33.name+',今年'+obj33.age+',性别'+obj33.sex+',爱好'+obj33.hobby+'。';
console.log(str4);        //大家好,我叫:zhuzhu,今年18,性别男,爱好女。// 但是字符串的拼接太麻烦了,有没有简单的方法来解决这个问题呢,有的,模板字符串就可以了var temp111 = `大家好,我叫${obj33.name},今年${obj33.age},性别${obj33.sex},爱好${obj33.hobby}`;
console.log(temp111);    //大家好,我叫zhuzhu,今年18,性别男,爱好女//1,可以是变量let name8 = "美女";
let temp22 = `我叫${name8}`;
console.log(temp22);    //我叫美女// 2,可以是方法function getName(){    return "宝宝";
}
let temp3 = `我叫${getName()}`;
console.log(temp3);        //我叫宝宝// 3,可以是表达式let aa = 1 ;
let bb = 2;
let temp4 = `a + b=${aa+bb}`;
console.log(temp4);        //a + b=3
Copy after login

Five, 6.0 Arrow function: ( )=>{}


// 最原始函数var arr = [2,3,5,7];
 $(arr).each(function(index,item){
   console.log(item);
 }); // 箭头函数改造// 改造一:匿名函数中的funtion关键字我们可以省略,参数与方法体之间中=>$(arr).each((index,item)=>{console.log(item);})
 // 改造二:如果方法体中的代码只有一句我们可以去掉{},并且代码结尾的分号要去掉$(arr).each((index,item) =>console.log(item));
Copy after login

The above is the detailed content of Detailed syntax of ES6. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Detailed explanation of obtaining administrator rights in Win11 Detailed explanation of obtaining administrator rights in Win11 Mar 08, 2024 pm 03:06 PM

Windows operating system is one of the most popular operating systems in the world, and its new version Win11 has attracted much attention. In the Win11 system, obtaining administrator rights is an important operation. Administrator rights allow users to perform more operations and settings on the system. This article will introduce in detail how to obtain administrator permissions in Win11 system and how to effectively manage permissions. In the Win11 system, administrator rights are divided into two types: local administrator and domain administrator. A local administrator has full administrative rights to the local computer

Detailed explanation of division operation in Oracle SQL Detailed explanation of division operation in Oracle SQL Mar 10, 2024 am 09:51 AM

Detailed explanation of division operation in OracleSQL In OracleSQL, division operation is a common and important mathematical operation, used to calculate the result of dividing two numbers. Division is often used in database queries, so understanding the division operation and its usage in OracleSQL is one of the essential skills for database developers. This article will discuss the relevant knowledge of division operations in OracleSQL in detail and provide specific code examples for readers' reference. 1. Division operation in OracleSQL

Detailed explanation of the role and usage of PHP modulo operator Detailed explanation of the role and usage of PHP modulo operator Mar 19, 2024 pm 04:33 PM

The modulo operator (%) in PHP is used to obtain the remainder of the division of two numbers. In this article, we will discuss the role and usage of the modulo operator in detail, and provide specific code examples to help readers better understand. 1. The role of the modulo operator In mathematics, when we divide an integer by another integer, we get a quotient and a remainder. For example, when we divide 10 by 3, the quotient is 3 and the remainder is 1. The modulo operator is used to obtain this remainder. 2. Usage of the modulo operator In PHP, use the % symbol to represent the modulus

Detailed explanation of the linux system call system() function Detailed explanation of the linux system call system() function Feb 22, 2024 pm 08:21 PM

Detailed explanation of Linux system call system() function System call is a very important part of the Linux operating system. It provides a way to interact with the system kernel. Among them, the system() function is one of the commonly used system call functions. This article will introduce the use of the system() function in detail and provide corresponding code examples. Basic Concepts of System Calls System calls are a way for user programs to interact with the operating system kernel. User programs request the operating system by calling system call functions

Detailed explanation of Linux curl command Detailed explanation of Linux curl command Feb 21, 2024 pm 10:33 PM

Detailed explanation of Linux's curl command Summary: curl is a powerful command line tool used for data communication with the server. This article will introduce the basic usage of the curl command and provide actual code examples to help readers better understand and apply the command. 1. What is curl? curl is a command line tool used to send and receive various network requests. It supports multiple protocols, such as HTTP, FTP, TELNET, etc., and provides rich functions, such as file upload, file download, data transmission, proxy

Detailed analysis of C language learning route Detailed analysis of C language learning route Feb 18, 2024 am 10:38 AM

As a programming language widely used in the field of software development, C language is the first choice for many beginner programmers. Learning C language can not only help us establish the basic knowledge of programming, but also improve our problem-solving and thinking abilities. This article will introduce in detail a C language learning roadmap to help beginners better plan their learning process. 1. Learn basic grammar Before starting to learn C language, we first need to understand the basic grammar rules of C language. This includes variables and data types, operators, control statements (such as if statements,

Detailed explanation of numpy version query method Detailed explanation of numpy version query method Jan 19, 2024 am 08:20 AM

Numpy is a Python scientific computing library that provides a wealth of array operation functions and tools. When upgrading the Numpy version, you need to query the current version to ensure compatibility. This article will introduce the method of Numpy version query in detail and provide specific code examples. Method 1: Use Python code to query the Numpy version. You can easily query the Numpy version using Python code. The following is the implementation method and sample code: importnumpyasnpprint(np

Learn more about Promise.resolve() Learn more about Promise.resolve() Feb 18, 2024 pm 07:13 PM

Detailed explanation of Promise.resolve() requires specific code examples. Promise is a mechanism in JavaScript for handling asynchronous operations. In actual development, it is often necessary to handle some asynchronous tasks that need to be executed in sequence, and the Promise.resolve() method is used to return a Promise object that has been fulfilled. Promise.resolve() is a static method of the Promise class, which accepts a

See all articles