1. Short cycle:
Shorten the loop in one line and write less code for the loop
var names=[ 'xinyu', 'xinyuya', 'xinyuyou' ] Original code: for( var i=0,i<names.length;i++ ){ console.log( names[i] ) } After shortening the code: for ( let name of names names ) console.log( name )
2. Adjust the length of an array
Length is not only used to get the size of the array. If we set the length of the array to any number, it will slice the array
var array1=[ 1,2,3,4,5,6 ] var array2=[ 'xinyu','xinyuya','xinyuyou' ] array1.length=3 array2.length=1 console.log( array1 )// [1,2,3] console.log( array2 )// ['xinyu']
3. Function parameters
There is no need to define function parameters. You can use the function as an array object without declaring parameters when the function is implemented
function add( ){ var sum=0; for( var i=0;i<arguments.length;i++ ){ sum=sum+arguments[i] } console.log( 'Total Sum :',sum )// Total Sum : 9 } add( 1,3,5 )
4. Timestamp in JavaScript
In JavaScript, there are many ways to use the Date method to get the Date
var date=new Date() timetamp=date.getDate() console.log( timetamp ) timetamp=new Date().getDate() console.log(timetamp) timetamp+=new Date() console.log( timetamp )
5. Delete a value in the array
Usually, we use the delete method to delete an item from the array, but this is the method of punching in the array. It puts undefined on the deleted item index
You can use the splice method to do some work, but it will completely delete the index from the array without leaving any vulnerabilities
var array=[ 1,2,3,4,5 ] delete array[4] array.splice(4,1) console.log( array ) // [ 1,2,3,4,5 ]
6. IN operator IN JavaScript
By using the IN operator, you can check whether there are keys IN the object. When you check whether there are specific keys IN the object, use the following techniques
var a=4; var b=5; var list={ 1;7, 3:9, 4:0, 2:9 } console.log( a in list ) // true console.log( b in list ) // false
7.JavaScript string filling
JavaScript padding is used to add padding to string text. Padding can be added at the beginning or end of a string
The following is the syntax of padStartand padEnd
padStart( targetLength,padString(optional)) padEnd( targetLength,padString(optional))
padString is an optional parameter in the two padding methods
The following is a code example to understand their work
console.log('123'.padStart(5)) // 123 console.log('123'.padStart(5,'0')) // 00123 console.log('123'/padEnd(5,'0')) // 12300 console.log('123'.padEnd(10,'0)) // 1230000000
8.Power * * operator
This feature will save a lot of mathematical calculation time. You may use math Pow() function to calculate the power of a number
But we can use the * * operator instead
Original: var p=Math.pow( 2,5 ) console.log( p ) // 32 Now: var p=2**5 console.log( p ) // 32 Original: var p = Math.pow(2,5) + Math.pow(2,5) + Math.pow(2,1) + Math.pow(2,3) + Math.pow(2,4) + Math.pow(2,9) console.log(p) // 602 Now: is it more concise and clear? var p=2**5 + 2**5 + 2**1 + 2**3 + 2**4 + 2**9 console.log(p) // 602
9.includes()
Most people use indexOf to find elements in an array?
No,
There are better ways to do the same job,
Use the include method instead of indexOf to return a boolean result
var array=[ 'xinyu','xinyuya','xinyuyou' ] console.log( array.includes( 'xinyuya' ) // true console.log( array.includes( 'xin' ) // false
10. Redirect to URL
JavaScript has some ways to redirect to the web site in the browser after executing the code
When the user performs any action on the website and JavaScript redirects the user to another URL,
const Redirect = (url, asLink = true) => asLink ? (window.location.href = url) : window.location.replace(url); redirect('https://medium.com/@codedev101')
11. Unary operator (+)
Unary operators can easily convert string numbers to numeric format and date to milliseconds
var strNum ='123.3' var num=+strNum console.log( num ) // 123.3 var currentDate=new Date() var millisSince=+currentDate console.log(millisSince)
12. Convert floating point numbers to integers (fast method)
To convert a floating-point number to an integer, you must use math floor() Math.round() and math Ceil() method
You can also use the | push-pull OR operator to convert in a faster way
Original: console.log(Math.floor(23.56)) Now: console.log( 23.56|0 )
over
If it's helpful to you, please give me a praise and encouragement~