Multiple usage tips for JavaScript ternary operators _ javascript skills

Source: Internet
Author: User
This article mainly introduces a variety of usage techniques of JavaScript ternary operators. This article describes common usage, usage of a little smarter, and even more unusual usage, if you need more code, you may unconsciously replace if else with three elements when you find that the code is too much to write. this simply does not make the code more concise and incisive, of course, some people say that the use of three yuan can make you feel a climax. I recently felt this way when I was writing JavaScript, and I collected some tips and shared them.

Please skip the following section, and the Big Bird will help you correct it ^__ ^

==== Popularity ====

Expression (expr1 )? (Expr2): (expr3)

When the value of expr1 is TRUE, the value is expr2, and when the value of expr1 is FALSE, the value is expr3.

================

Common usage

When you find that you often use if else

The code is as follows:


If (Bai Chunge | Bai Xuedi ){
Do not go to the subject;
} Else {
Door mounting;
}


The three-element representation is

The code is as follows:


Baichun brother | Baishi test Emperor? No mounting: door mounting


The code is brilliant.

Such if else judgments are often made in daily use. in particular, it is more harmonious to use the ternary element when there are many nesting elements, which can make your code look more fresh and have a clear structure.

Usage with a little cleverness
Through constant changes, many ternary usage can be derived. The following jquery code

The code is as follows:


Flag? $ ('Body'). addClass ('hover '): $ ('body'). removeClass ('hover ');


Even more abnormal.

The code is as follows:


$ ('. Item') [flag? 'Addclass': 'removeclass '] ('hover ')


The above code looks confusing. When flag is set to true, the code becomes the following code:

The code is as follows:


$ ('. Item') ['addclass'] ('hover ')


This statement is equivalent.

The code is as follows:


$ ('. Item'). addClass ('hover ')

Please try again

You can call the desired function as needed to handle more things.

The code is as follows:


Function (){
Do something
}
Function B (){
Do something
}

Flag? A (): B ();

Therefore, it is the complete body of the teacher.

So in this case, two buttons act forward and backward. The operation functions are similar.

The code is as follows:


Var action_turn = function (e, type ){
Var self = $ (e). closest ('Lil ');
Var target = self [type = 'prev '? 'Prev': 'Next'] ();
Target. addClass ('Has-img ');
Self. removeClass ('Has-img ')
}

Var btn_next = $ ('# item-photo-panel a. next ')
Btn_next.click (function (){
Action_turn (this, 'next ');
Return false;
});
Var btn_prev = $ ('# item-photo-panel a. prev ')
Btn_prev.click (function (){
Action_turn (this, 'prev ');
Return false;
});


Situations to be avoided as much as possible

The code is as follows:


Alert (true? 'True': false? 'T': 'F ')


I mean try to avoid the three elements nested above, because in js, the statements are from right to left, and the above code is equivalent

The code is as follows:


Alert (true? 'True': (false? 'T': 'F '))


For example, the results in php are completely different, and the left-side priority is given when the three elements are nested.

The code is as follows:


Echo (true? 'True': false )? 'T': 'F') // php


Tip:
In addition, we found that the trielement in php had such a prompt.

Note: the ternary operator is a statement. Therefore, the result is not a variable but a statement. It is important to return a variable through reference. Return $ var = 42? $ A: $ B; will not work, and a warning will be issued for future PHP versions.

However, after testing, it is found that the above practices in javascript can work, probably because js is relatively BT and its degree of rigor is not as big as php.

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.