[iGeek Manual] How to write more concise javascript code?

 


 

In the process of writing Javascript code, we often use overly complicated code and syntax. In this article, we have collected 10 examples of writing javascript code quickly, hoping to help everyone!

 

Big data writing skills

If you write relatively large data, you often need to define a lot of 0s, for example, 1000000, but in javascript, you can use e to represent 0, the following is an actual comparison example:

 

Redundant version code:

var sum = 1000000;

Condensed version code:

var sum = 1e6

 

If you define 3800000 , you can use this shortcut notation  38e5

 

Number increment and decrement

This technique, you should use relatively more, is often used in for loops, as follows:

 

Redundant version code:

i=i+1;
j=j-1;

Condensed version code:

i++
j--

 

addition, subtraction, multiplication and division

The related basic algorithm symbols have similar abbreviated codes that can quickly help us complete the code, as follows:

Redundant version code:

i=i+5;
d=d-3;
k=k*10;
l=l/3;

Condensed version code:

i+=5;
j-=3;
k*=10;
l/=3;

 

Determine character position

In character processing, charAt is often used to locate conceit, but we can often use a more concise way to locate character positions, as follows:

 

Redundant version code:

myString.charAt(14);

Condensed version code:

myString[14];

 

define variable

This should be used more often, as follows:

Redundant version code:

var i;
var j = 15;
var k="Good Geek";
var 1;
var bb = true;

Condensed version code:

var i, j=15, k="Good Geek", l, bb=true;
 

Defining relational data

In addition to simple arrays, javascript commonly used associative arrays can be written more concisely, as follows:

 

Redundant version code:

var myArray = new Array();
myArray["Grace Kelly"] = "Philadelphia";
myArray["Clint Eastwood"] = "San Francisco";
myArray["Humphrey Bogart"] = "New York City";
myArray["Sophia Loren"] = "Rome";
myArray["Ingrid Bergman"] = "Stockholm";

Condensed version code:

var myArray = {
  "Grace Kelly": "Philadelphia",
  "Clint Eastwood": "San Francisco",
  "Humphrey Bogart": "New York City",
  "Sophia Loren": "Rome",
  "Ingrid Bergman": "Stockholm"
}

 

define object

The process of defining objects can also use a simpler version, as follows:

 

Redundant version code:

var myObj = new Object();
myObj.name = "Sean Connery";
myObj.placeOfBirth = "Edinburgh";
myObj.age = 86;
myObj.wasJamesBond = true;

Condensed version code:

var myObj = { name: "Sean Connery", placeOfBirth: "Edinburgh",
age: 86, wasJamesBond: true };

 

Use conditional symbols

If you use the if else syntax, you may also be able to write it in a simpler way, as follows:

 

Redundant version code:

var message = '';
if(age>=18){
  message = "Allowed";
}else{
  message = "Denied";
}

Condensed version code:

var message = age >= 18 ? "Allowed" : "Denied";

 

Check if a variable exists

To determine whether a variable exists, it can also be written in a simpler way:

 

Redundant version code:

var myVar = 55 ;
if( typeof myVar !== "undefined" && myVar !==  "" && myVar !== null
&& myVar !== 0 && myVar !== false  ) {
  console.log("The myVar variable is defined AND it's not empty
  AND not null AND not false.");
}

Condensed version code:

var myVar = 55 ;
if( myVar ) {
  console.log("The myVar variable is defined AND it's not empty
  AND not null AND not false.");
}

 

Determine whether a variable is assigned a value

 

Redundant version code:

var myVar;
if( typeof myVar === "undefined" || myVar === "" || myVar === null
|| myVar === 0 || myVar === false  ) {
  console.warn("The myVar variable is undefined (OR) empty (OR)
  null (OR) false.");
}

Condensed version code:

var myVar;
if( !myVar ) {
  console.warn("The myVar variable is undefined (OR) empty (OR)
  null (OR) false.");
}

 


 

The above are some quick writing codes that are often used in javascript, I hope it will be helpful to everyone~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326485358&siteId=291194637