Big data writing skills
var sum = 1000000;
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:
i=i+1; j=j-1;
i++
j--
addition, subtraction, multiplication and division
i=i+5; d=d-3; k=k*10; l=l/3;
i+=5; j-=3; k*=10; l/=3;
Determine character position
myString.charAt(14);
myString[14];
define variable
This should be used more often, as follows:
var i; var j = 15; var k="Good Geek"; var 1; var bb = true;
var i, j=15, k="Good Geek", l, bb=true;
Defining relational data
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";
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:
var myObj = new Object(); myObj.name = "Sean Connery"; myObj.placeOfBirth = "Edinburgh"; myObj.age = 86; myObj.wasJamesBond = true;
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:
var message = ''; if(age>=18){ message = "Allowed"; }else{ message = "Denied"; }
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:
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."); }
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
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."); }
var myVar; if( !myVar ) { console.warn("The myVar variable is undefined (OR) empty (OR) null (OR) false."); }