Clarifying Boolean Parameters, part 2

Part 1 is here

Some languages have better ways to pass boolean parameters.  C# 4.0, and all versions of VB, allow parameters to be passed by name.  This allows us to write much clearer code:

//C# 4.0:
UpdateLayout(doFullLayout: false) 

Without requiring any changes to the function definition, this makes the meaning of the true / false abundantly clear at the call-site.

Javascript offers another interesting alternative.  In Javascript, booleans conditions actually check for “truthyness”.  The statement if(x) will trigger  not just if x is true, but also if x is any “truthy” value, including any object, non-empty string, or non-zero number. Similarly, the expression !x will return false if x is “truthy” and true if x “falsy”.

This means that we can actually use any non-empty string instead of true in Javascript.  Note that this will only work if the function checks the value for “truthyness”; it won’t work for code like if (x === true).

Thus, instead of passing true as a boolean, you can pass a string that describes what you’re actually indicating.

For example:

function updatePosition(animate) {
    //Calculate position
    if (animate)

$(window).resize(function() {

updatePosition("With animation");

Although this results in much more readable code, it can be difficult to understand for people who aren’t familiar with this trick.  If the meaning of the parameter changes, you’ll need to hunt down every place that the function is called and change the string to reflect the new meaning.

Finally, unlike an enum, this does not scale to multiple options.  If you need to have more than two options, you should use global variables or objects to simulate an enum, not strings.


It's nice that someone shares them.
pożyczka przez internet

The above article is nice and interesting, thank you willing to share! Greetings success of admin Percetakan Murah Rawamangun Jakarta Timur wish you deign to visit my website, thank you :)

Business success depends on many factors, but I consider knowledge and experience to be the most important. Now I am developing a project that helps in online education quickly and efficiently, much more depends on your team. It should be composed of the best experts and professionals.

Yes, I have agreed with you that JavaScript has the ability to pass the Boolean parameters. Although I am not a pro in languages, when I took the best reliable thesis writing services for my computer science thesis, I learned a lot about it. As JavaScript treats empty string, 0, null, and undefined as false, and everything else is true.

By looking at the website write my term paper the student will receive many offers from the best performers, each of whom declares their cost for the performance of this work. Therefore, the customer will be able to choose the author's pricing policy, which will correspond to his financial capabilities.

Hey! Nice article gues will be useful for all students who are currently passing their exams. Should say that this period is the most stressing for each student, numbers of home assignments, deadlines which should be closed till the exams and actually exams themselves. So, if you are you having nightmares over academic homework you can use my freshessay writing service and mine help as a qualified writer.

Hi) Thanks for sharing this interesting information with us! If you need to buy research proposal for a low price you can visit the site Place-4-Papers and a team of professionals will help you

This blog is helpful for computer science students. I am working as a writer and offer to buy assignment online. I must say that these types of blogs are an excellent source for students, and they provide help in their academic work.

There is no doubt that Java script has an ability to pass Boolean parameters, I learn about Java script in my office at crypto currency exchange platform development company where we provide the best and most reliable crypto exchange software.

Post a Comment