JavaScript shorthand techniques reproduced continuous handling, aspects of their own view

Source: Internet
Author: User
Tags new set

Tips on some JavaScript, reproduced from HTTPS://WWW.GEEKJC.COM/POST/5A0A8C9A592E38541F7703C8.

The 1IF statement can be abbreviated with a three-mesh operation:

const x = 20;

if (x > 10) {

} else {
Answer = ' less than 10 ';
}

Const ANSWER = x > 10? ' Greater than ': ' less than 10 ';

  

2. String concatenation:

var arr = [{    "name": "Dachun",    "age": +, "    sex": "Male"       }];for (Let i = 0;i<arr.length;i++) {    Console.log ("Name:" + Arr[i].name + "Age:" + Arr[i].age + "Gender:" + Arr[i].sex);}   ES6 string concatenation: for (Let i = 0;i<arr.length;i++) {console.log (' Name: ${arr[i].name} Age: ${arr[i].age} Gender: ${arr[i].sex} '); }//Note: In the ' Write variable, the ESC key below the English state can be pressed out '

 

3. Arrow functions

function SayHello (name) {  console.log (' Hello ', name);} SetTimeout (function () {  console.log (' Loaded ')}, () List.foreach (function (item) {  Console.log (item);}); /with arrow function shorthand:  SayHello = name + console.log (' Hello ', name);  SetTimeout (() = Console.log (' Loaded ');  List.foreach (item = Console.log (item));

 

4, the array to go to the weight (the key qaq the question several times have this)

var arr = [1, 1, 2, 2, 3, 3];                     function Unique (arr) {let    res = [];    for (var i = 0;i<arr.length;i++) {for (var j = 0; j<res.length;j++) {    if (arr[i] = = = Res[j]) {break;    }}  if (j = = = Res.length) {    res.push (arr[i]);}   } return res; }console.log (arr);             Es6 Set method function unique (arr) {return array.from (new Set (arr));   } Console.log ("Es6set method:", Unique (arr));    

  

  

JavaScript shorthand techniques reproduced continuous handling, aspects of their own view

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.