一、变量声明#
let x;
let y;
let z = 520 ;
let x, y, z = 520 ;
二、三元运算符#
let num1 = 520 ;
let num2 = 1314 ;
if ( num1 > num2) {
} else {
}
let result = num1 > num2 ? true : false ;
三、解构赋值#
let a, b, c;
a = 1 ;
b = 2 ;
c = 3 ;
let [ a, b, c] = [ 1 , 2 , 3 ] ;
四、解构交换#
let x = '极客飞兔' , y = '程序员' ;
const temp = x;
x = y;
y = temp;
[ x, y] = [ y, x] ;
五、箭头函数#
function add ( num1, num2 ) {
return num1 + num2;
}
const add = ( num1, num2 ) => num1 + num2;
六、字符串模版#
console. log ( '极客飞兔的年龄 ' + age + ' 他的身高 ' + height) ;
console. log ( ` 极客飞兔的年龄 ${ age} 他的身高 ${ height} ` ) ;
七、多值匹配#
if ( value === 1 || value === '飞兔' || value === 2 || value === '程序员' ) {
}
if ( [ 1 , '飞兔' , 2 , '程序员' ] . indexOf ( value) >= 0 ) {
}
if ( [ 1 , '飞兔' , 2 , '程序员' ] . includes ( value) ) {
}
八、ES6对象简写#
let firstname = '极客' ;
let lastname = '飞兔' ;
let userinfo = { firstname: firstname, lastname: lastname} ;
let userinfo = { firstname, lastname} ;
九、字符串转数字#
let total = parseInt ( '520' ) ;
let average = parseFloat ( '13.14' ) ;
let total = + '520' ;
let average = + '13.14' ;
十、次方相乘#
const power = Math. pow ( 2 , 5 ) ;
const power = 2 ** 5 ;
十一、数组合并#
let arr1 = [ 520 , 1314 ] ;
let arr2 = arr1. concat ( [ 1225 , 1115 ] ) ;
let arr2 = [ ... arr1, 1225 , 1115 ] ;
十二、查找数组最大值最小值#
const arr = [ 520 , 1314 , 1115 , 1225 ] ;
Math. max ( ... arr) ;
Math. min ( ... arr) ;
十三、获取字符串字符#
let str = 'https://autofelix.blog.csdn.net/' ;
str. charAt ( 10 ) ;
str[ 10 ] ;
十四、并&&操作#
function fn ( ) {
return true ;
}
let flag = true ;
if ( flag) {
fn ( ) ;
}
flag && fn ( ) ;
十五、数组排序#
const arr = [ 40 , 2 , 1 , 5 , 99 , 111 ] ;
arr. sort ( ( a, b ) => a - b) ;
arr. sort ( ( a, b ) => b - a) ;
十六、数组过滤#
const arr = [ 3 , '1' , '' , 0 , false , null , undefined ] ;
arr. filter ( Boolean) ;
十七、for循环#
let arr = [ '极客飞兔' , 520 , 1314 , '程序员' ]
for ( var i = 0 ; i < arr. length; i++ ) { }
for ( const i in arr) { }
for ( const i of arr) { }
十八、判断奇偶#
if ( value / 2 == 0 ) {
} else {
}
2 & 1 ;
3 & 1 ;
十九、数组去重#
const array = [ 5 , 4 , 7 , 8 , 9 , 2 , 7 , 5 ] ;
array. filter ( ( item, idx, arr ) => arr. indexOf ( item) === idx) ;
const nonUnique = [ ... new Set ( array) ] ;
二十、IF检查#
if ( result === true )
if ( result)
二十一、合并对象#
const user = {
name: '极客飞兔' ,
gender: '男'
} ;
const college = {
primary: '清华大学' ,
secondary: '社会大学'
} ;
const skills = {
java: 'JAVA' ,
php: 'PHP' ,
python: 'PYTHON'
} ;
const summary = { ... user, ... college, ... skills} ;
二十二、可选链#
const user = {
employee: {
name: "极客飞兔"
}
} ;
user. employee?. name;
user. employ?. name;
user. employ. name;
二十三、字符串重复#
let str= '' ;
for ( let i = 0 ; i < 5 ; i ++ ) {
str+= 'autofelix ' ;
}
'autofelix ' . repeat ( 5 ) ;
二十四、默认值#
let user;
let name = getUserName ( ) ;
if ( name !== null && name !== undefined && name !== '' ) {
user = name;
} else {
user = '极客飞兔' ;
}
let user = getUserName ( ) || '极客飞兔' ;
二十五、双波浪线运算符#
const floor = Math. floor ( 6.8 ) ;
const floor = ~ ~ 6.8 ;
二十六、移除对象属性#
let obj = { x: 45 , y: 72 , z: 68 , p: 98 } ;
delete obj. x;
delete obj. p;
console. log ( obj) ;
let { x, p, ... newObj} = obj;
console. log ( newObj) ;