Guide: Best Beginner JavaScript Shorthands

JavaScript abbreviations not just speed up make the coding process as well as scripts shorter, thus lead to faster page loadsShorthand codes are just as valid as their handwritten versions; them essentially stand for the same–Only in a more compact format. They are one of the simplest code optimization techniques. However, there are several JavaScript shorthand have no official reference guideSome are very simple, while others are quite intimidating, even for experienced developers. In this article you will find 10 JavaScript shorthand for beginners that allows you to start with code optimization and write more concise code.

1. Decimal numbers

If you are a regular work with large decimal places this shorthand can be a godsend, since you no longer have to enter all zeros, just replace them with the e-notation. For example, 1e8 means adding eight zeros after the 1 digit, it equals 100000000. The number after the letter e indicates the number of zeros they come after the digit (s) before e. Likewise, 16e4 is the abbreviation for 160000, etc. / * Shorthand * / var myVar = 1e8; / * Longhand * / var myVar = 100000000;

2. Increase, decrease

The increment shorthand is made up of two + characters, it means that the value must be of a variable increased by oneLikewise it is abbreviation shorthand consists of two characters, and it means that the variable must be decreased by one These two abbreviations can be used only on numeric data typesThey play an indispensable role in loops, their most common use case being the for loop / * Shorthand * / i ++; j–; / * By hand * / i = i + 1; j = j-1;

3. Addition, derivation, multiplication, division

There is an abbreviation for each of the four basic math operations: addition, derivation, multiplication and division. They work the same way as the increase and decrease operators, only here you can change the value of a variable through any number (not only per piece). In the example below, the variable i is increased by 5, j is decreased by 3, k is multiplied by 10, and l is divided by 2. / * Abbreviation * / i + = 5; j- = 3; k * = 10; 1 / = 2; / * By hand * / i = i + 5; j = j-3; k = k * 10; l = 1/2;

4. Determine the character position

The charAt () method is one of the most commonly used string methods, it returns the draw at a specified position (for example, the 5th character of a string). There’s a simple abbreviation you can use instead: you add the character position in square brackets after the string. Note that the method is charAt () zero-basedTherefore myString[4] returns the 5th character in the string (“y” in the example). var myString = “Happy birthday”; / * Shorthand * / myString[4] / * Longhand * / myString.charAt (4);

5. Declare variables in bulk

If you want to create more than one variables at a time you don’t have to type them out one by one. It is enough to use the keyword var (or let) only once, then you can just state the variables you want to create, separated by a comma With this shorthand you can indicate both undefined variables and variables with a value / * Shorthand * / var i, j = 5, k = ”Good morning”, l, m = false; / * Longhand * / var i; var j = 5; var k = “Good morning”; var l; var m = false;

Declare an associative array

Declaring an array in JavaScript is a relatively simple task by using the var myArray = [“apple”, “pear”, “orange”] syntax. However, declare an associative array is a bit more complicated, because here you need to define not only the values, but also the keys (in the case of regular arrays, the keys are 0, 1, 2, 3, etc.). An associative array is one collection of key-value pairsThe long way is to declare the array and then add each element one by one. However, you can also use the abbreviation below declare the associative array plus all its elements at the same time. In the example below, the associative array myArray assigns their birthplace (values) to known people (keys). / * Shorthand * / var myArray = {“Grace Kelly”: “Philadelphia”, “Clint Eastwood”: “San Francisco”, “Humphrey Bogart”: “New York City”, “Sophia Loren”: “Rome”, “Ingrid Bergman “:” Stockholm “} / * Longhand * / 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”;

7. Indicate an object

The abbreviation for object statement works in the same way as for associative arrays. Here, however, there are no key / value pairs yet property-value pairs that you have to place between the brackets {}. The only difference in the abbreviated syntax is that object properties are not enclosed in quotation marks (name, placeOfBirth, age, wasJamesBond in the example below). / * Shorthand * / var myObj = {name: “Sean Connery”, placeOfBirth: “Edinburgh”, age: 86, wasJamesBond: true}; / * Longhand * / var myObj = new object (); = “Sean Connery”; myObj.placeOfBirth = “Edinburgh”; myObj.age = 86; myObj.wasJamesBond = true;

8. Use the conditional operator

The conditional (ternary) operator is often used as the shortcut for the if-else statementIt consists of three parts In the example below, we will send a simple message (within the message variable) to people who want to enter a club. Using the shorthand form, it is just one line of code to run the evaluation var age = 17; / * Shorthand * / var message = age> = 18? “Allowed”: “Denied”; / * Longhand * / if (age> = 18) {var message = “Allowed”;} else {var message = “Denied”;} If you just want to test it copy the code to the web console (F12 in most browsers) and change the age variable value a few times.

9. Check attendance

It often happens that you have to check whether a variable is present or notThe “As presence” shorthand helps you do this with much less code. Beware that most articles on JavaScript shorthand don’t provide proper hand-crafted form, as the if (myVar) notation checks not only that the variable isn’t false, but a handful of other things as well. Namely the variable cannot be undefined, empty, null and false var myVar = 99; / * Shorthand * / if (myVar) {console.log (“The myVar variable is defined AND it is not empty AND not null AND not false.”);} / * Longhand * / if (typeof myVar! == “undefined” && myVar! == “” && myVar! == null && myVar! == 0 && myVar! == false) {console.log (“The myVar- variable is defined AND it is not empty AND not zero AND not false. “);} You can test how the shorthand “as presence” works by inserting the following snippet of code into the web console and changing the value of myVar a few times. To understand how this abbreviation works, it is worth testing it with the values ​​”” (empty string), false, 0, true, a non-empty string (eg “Hi”), a number (eg . 99), and when the variable is undefined (just var myVar;).

10. Check absence

The abbreviation “as presence” can be used check the absence of a variable by posting an exclamation mark in front of itThe exclamation mark is it logically not operator in JavaScript (and in most programming languages). Therefore, the if (! MyVar) notation allows you to check the myVar variable is not undefined, empty, null or false var myVar; / * Shorthand * / if (! MyVar) {console.warn (“The myVar variable is undefined (OR) empty (OR) null (OR) false.”);} / * Longhand * / if (typeof myVar === “undefined” || myVar === “” || myVar === null || myVar === 0 || myVar === false) {console.warn ( The myVar variable is undefined (OR) blank (OR) zero (OR) false. “);}

Best Beginner JavaScript Shorthands: benefits


Final note

I hope you like the guide Best Beginner JavaScript Shorthands. In case if you have any query regards this article you may ask us. Also, please share your love by sharing this article with your friends. For our visitors: If you have any queries regards the Best Beginner JavaScript Shorthands, then please ask us through the comment section below or directly contact us. Education: This guide or tutorial is just for educational purposes. Misinformation: If you want to correct any misinformation about the guide “Best Beginner JavaScript Shorthands”, then kindly contact us. Want to add an alternate method: If anyone wants to add more methods to the guide Best Beginner JavaScript Shorthands, then kindly contact us. Our Contact: Kindly use our contact page regards any help. You may also use our social and accounts by following us on Whatsapp, Facebook, and Twitter for your questions. We always love to help you. We answer your questions within 24-48 hours (Weekend off). Channel: If you want the latest software updates and discussion about any software in your pocket, then here is our Telegram channel.

Best Beginner JavaScript Shorthands 2021  2022  - 70Best Beginner JavaScript Shorthands 2021  2022  - 27Best Beginner JavaScript Shorthands 2021  2022  - 50Best Beginner JavaScript Shorthands 2021  2022  - 94