Home > Article > Web Front-end > Skills you need to master in JavaScript
If you are new to JavaScript or have only recently encountered it in your development work, you may be feeling frustrated. All languages have their quirks - but developers moving from a server-side language based on strong typing can be confused. I was like that, a few years ago, when I was thrust into becoming a full-time JavaScript developer, and there were a lot of things I wish I'd known at the beginning. In this article, I'm going to share some of my quirks and hopefully I can share with you some of the experiences that have given me a lot of headaches. This isn't a complete list - just a partial list - but hopefully it opens your eyes to the power of this language, and things you may have once considered a hindrance.
When we create a new variable, we usually verify whether the value of the variable is null (null) or undefined. This is a verification that often needs to be considered for JavaScript programming.
If you write it directly, it would look like this:
##if (variable1 !== null || variable1 ! == undefined || variable1 !== '') { let variable2 = variable1; }
|
##let variable2 = variable1 || '';
|
If you don’t believe it, try it in the console of the Google Chrome developer panel !
//Example of null value let variable1 = null;
let variable2 = variable1 || '';
console.log(variable2);//Example of undefined value let variable1 = undefined;
let variable2 = variable1 || '';
console.log( variable2);//Output: '' //Normal situation let variable1 = 'hi there';
let variable2 = variable1 || '';
console.log(variable2);//Output: 'hi there' |
2. Array
let a = new Array(); a[0] = "myString1"; a[1] = "myString2"; a[2] = "myString3";
|
"myString1", "myString2", "myString3"];
|
if (x > 10) {
big = true;
}
else {
big = false;
}
|
true : false;
Greatly simplifies the amount of code!
3;
1;minusCount = minusCount -
minusCount -- ; y*= 10;
// x=15
var re = "\d+(.)+\d+", |
result = re.exec("padding 01234 text text 56789 padding");console.log(result) ; //"01234 text text 56789"
##After simplification:
|
7. If condition optimizationAlthough it is very simple, it is still worth mentioning. Before simplification:
|
##if (likeJavaScript ===
true)
Simplified: | if (likeJavaScript)
if ( c!=
true ) {
}
After simplification:
9. Function parameter optimizationI personally tend to use the method of obtaining object elements to access function parameters. Of course, this is a matter of opinion! Usually used version:
means 10000000.Before simplification:
|
The above is the detailed content of Skills you need to master in JavaScript. For more information, please follow other related articles on the PHP Chinese website!