9 useful abbreviations in the JavaScript syntax
- June 17, 2018

Various shortcuts increase your productivity and make the JavaScript code shorter. And since short records are absolutely equivalent to long ones, loading pages is faster. Thus, the use of abbreviations can be called one of the techniques of code optimization.
Most of the abbreviations described below are very simple but they are no less effective.

  1. Decimal numbers
  2. When you work with numbers, you constantly encounter an incredible number of zeros after a significant figure. In JavaScript, there is a way to abolish them by replacing physical symbols with their number. Thus, the number 20 000 000 will look like 2e7. Similarly, 37e4 is 370,000.

  3. Increment and decrement
  4. It would seem that the basic operation, which all must be brought to automatism. But no, still you look at the code and you meet
    x = x + 1;
    No one gets confused if you write x ++ instead, so do not ignore the 2 character save. The decrease by one looks similar.

  5. Multiplication division, addition, subtraction
  6. However, in the mandatory list of use, you must add not only a decrease or increase in the variable by one, but also any arithmetic operations with numbers:

    x + = 4; is equivalent to x = x + 4;
    x * = 4; is equivalent to x = x * 4;
    x / = 4; is equivalent to x = x / 4;

  7. Specifying a character position
  8. The charAt () method is one of the most commonly used in JavaScript. It returns a character at the specified position in parenthesis. That is, if you need to know the 5th character of a line – write:
    myString.charAt (4); // in charAt the characters are counted from zero.
    However, there is an entry much easier, and you probably know about it:
    myString [4]

  9. Multiple variable declarations
  10. If you need to create several variables, it is not necessary to stretch the record into several lines.
    It is enough to use the comma-delimited division and write it all in one line:
    var i, j = 1, k = “Hello”, l, m = false;
    As a result, you get a significant saving of space. If you are afraid of losing in the readability – you need to order the variables alphabetically, this will shorten the time when searching for values.

  11. Associative arrays
  12. If you want to create an array with known values, you just write:
    var myArray = ["A", "B", "C"];
    This is the simplest case, where by default keys for each word are position numbers, that is, 0, 1, 2, respectively. However, in JavaScript, you have the ability to create associative arrays – here the key can take any value. Since there is no order in this case, many beginners begin to create key-value pairs like this:
    var myArray = new Array ();
    myArray ["BigCity"] = "Moscow";
    myArray ["CultCity"] = "Saint Petersburg";
    myArray ["WarmCity"] = "Sochi";
    myArray ["FarCity"] = "Vladivostok";
    But you can easily avoid multiple copying of the array name by using the same entry:
    var myArray = {
    "BigCity": "Moscow",
    "CultCity": "Saint Petersburg",
    "WarmCity": "Sochi",
    "FarCity": "Vladivostok"
    }

    When you declare in braces, you write down all key-value pairs via a colon, and separate the pairs with commas.

  13. Object declaration
  14. The described form of the record can be applied not only to the creation of associative arrays, but also to the description of the object. Let’s remember how it’s customary to write down:
    var myObj = new Object ();
    myObj.name = "Sean Connery";
    myObj.placeOfBirth = "Edinburgh";
    myObj.age = 86;
    myObj.wasJamesBond = true;

    The same can be written when declared in curly brackets, but unlike associative arrays for parameters, double quotes are not used:
    var myObj = {name: "Sean Connery", placeOfBirth: "Edinburgh", age: 86, wasJamesBond: true};

  15. Ternary operator
  16. The classical construction of a ternary operator:
    condition;
    what happens if the condition is satisfied;
    what happens if the condition is not met.
    In the code it looks like this:
    if (age> = 21) {
    var message = "Allowed";
    } else {
    var message = "Denied";
    }

    This record can be placed in one line, reducing it to several characters:
    var message = age> = 21? "Allowed": "Denied";

  17. Check existence

Another popular operation is checking a variable, whether it was declared, whether it was assigned a value. You can do it like this:
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.");
}

In those cases when it is necessary to perform a check on all these items, the code can be greatly reduced:
if (myVar) {
console.log ("The myVar variable is defined AND it's not empty AND not null AND not false.");
}

Unfortunately, in such an entry, you can not check any additional conditions. Only changing the state of the variable from the default value. But it is possible to do the opposite operation, having found out the correspondence to one of the conditions:
if! myVar) {
console.warn ("The myVar variable is undefined (OR) empty (OR) null (OR) false.");
}

So you check that the variable is either not declared, or empty, or zero, or false.

Using these simple transformations will positively affect on the code and your professional skills.

recommended
Smart Order Router. What is this?

One of the key features of FinSafe’s trading platform is Smart Order Routing. A professional trading platform that a traditional …

9 useful abbreviations in the JavaScript syntax

Various shortcuts increase your productivity and make the JavaScript code shorter. And since short records are absolutely equivalent to long …

Several reasons to explore Swift

Swift is applied to create applications for Mac and iOS acting as a possible replacement for Objective-C. The latter, though …