[JavaScript Practice]--"Kinky tricks" you don't know.

Source: Internet
Author: User
Tags chrome developer

1. Null (null, undefined) validation

At first, I was a bit of a fool to verify (I really did)

if (variable1!== null | | variable1!== undefined | | variable1!== ") {Let variable2 = variable1;}

Big Brother taught me this, you'll marvel.

Let Variable2 = variable1  | | ‘‘;

If you don't believe it, try it under the console of the Google Chrome developer Dashboard!

Example of a null value let Variable1 = Null;let variable2 = variable1  | | "; Console.log (variable2);//output:"//value is undefined example let variable1 = Undefined;let variable2 = variable1  | | '; Console.log (variable2);//output: '//normal let variable1 = ' Hi there '; let Variable2 = Variable1  | | "; Console.log (variable2);//output: ' Hi there '

Isn't it great (cute face)

2. Arrays

This is often used, like I do not have a problem

Let a = ["MyString1", "MyString2", "MyString3"];
3. If true: Else's optimization

is actually using ternary arithmetic

Let big;if (x > Ten) {    big = true;} else {    big = false;}

It can turn out like this.

Let big = x > 10? True:false;

is not concise, ternary operations can be used in many places, (Vue inside to determine the loading of components,, is not irrelevant)

One more example.

< 5    = x > Ten, false:x < =10}; Console.log (big); "Object {truefalse=false}"
4. Variable declaration

Variable declarations, such as the Let X of the child; Let y;

It's as simple and clear as I am.

Let x, Y, z=3;
5. Simplification of assignment statements

At first, it was like this.

X=x+1;minuscount = minuscount-1;y=y*10;

Turned into

X++;minuscount--;y*=10;
6. Avoid using RegExpObject

Regular expressions, do you want to use the

var re = new RegExp ("\d+ (.)" +\d+ "," IgM "), result = Re.exec (" padding 01234 text text 56789 padding "); Console.log (result); "01234 text text 56789"

Simplify

var result =/d+ (.) +d+/igm.exec ("padding 01234 text text 56789 padding"); Console.log (result); "01234 text text 56789"
7. If Condition Optimization

Although it is very simple, it is worth mentioning.

if (likejavascript)

Is it better to be better than the word?

if (Likejavascript = = = True)

8. Function calls can also be shorter
function X () {Console.log (' x ')};function y () {console.log (' y ')};let z = 3;if (z = = 3) {    x ();} else {    y ();}

Actually, it's the function self-tuning.

function X () {Console.log (' x ')};function y () {console.log (' y ')};let z = 3; (z==3?x:y) ();
9. How to gracefully represent a large number

In JavaScript, there's a shorthand for a number, and maybe you're ignoring it. 1e7represents 10000000.

Before simplifying:

< 10000 ; i++) {

Le7 ..... ..... ..... ........................... ..... ..... ..... ..... .............. ........... (Naughty AH)

< 1e7 ; i++) {

What do you mean, four-odd kinky tricks?

Respect for our original author Ka

Copyright NOTICE: Please specify the author Fundebug and this address: https://blog.fundebug.com/2017/07/06/12-tricks-that-js-rocks/

[JavaScript Practice]--"Kinky tricks" you don't know.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.