Thomas’ Developer Blog

February 5, 2008

The question mark in javascript

Filed under: javascript — Tags: , , , — sanzon @ 1:57 am

Alright this is just something I figured that might be useful to help people out in javascript a lot.

 Ever see code like….

var targ = e.target?e.target:e.srcElement;

Generally the question that comes to mind what the heck is that quesiton mark for? Belive it or not it’s a short hand If statement.

Basically in this case it’s like saying if e.target exist then set variable targ to e.target. If not, “ELSE”, make targ equal to e.srcElement. This is really useful for cross browser issues.

Sadly, you can’t really use it in the AJAX code all that easily. Due to how many conditions you have to test against. But it’s really useful for other issues.

Also to clear some things up:

a=(a==b)?c:d

You can also write it out like that to mean… if a = b then set to c if NOT set to d.

Well hopes that helps you guys some. Also once I finish up on this drag element script I’ll be sure to update you with a really neat cross browser script for creating a draggable inbound window. Of course we’ll see how long it takes with my busy schedule.

25 Comments »

  1. Thanks for the info……ive been wondering for a while what that was…….

    Comment by Michael — December 3, 2008 @ 11:29 am

  2. Thanks dude, this came up on a script and I was like…what the hell???

    Comment by Camilo — April 30, 2009 @ 7:10 am

  3. I was hoping it’d be something magical.

    Comment by murrietadesign — November 3, 2009 @ 7:46 am

  4. One of the idiotic syntax part of JavaScript – badly readable!

    Tx for explanation!

    These JS developers were true idiots!

    Comment by Frank — December 13, 2009 @ 2:06 pm

  5. thanks, man

    Comment by nagarajan — February 24, 2010 @ 12:32 pm

  6. thanks a lot

    Comment by مازن الرمال — March 7, 2010 @ 11:03 am

  7. Thanks, it was hard to figure out for me. At least it helps saving space, something really important :)

    Comment by Esteban — February 1, 2011 @ 1:44 am

  8. This syntax looks really retarded!But many thanks for clarifying this. I really sometimes wonder why there is not _one_ standard of programming syntax and who comes up with “let’s shorten all operations possible!”. Soon you will just need a hex editor to type in your codes like the good ol 8bit mos6510 days..

    Comment by uSer — May 2, 2011 @ 11:24 am

  9. Thank you for explanation….I was looking for it.

    Comment by Anonymous — December 2, 2011 @ 11:21 am

  10. thank you for the explanation. this came in one of the code I was trying to modify.

    in my case it was something like this:

    this.lastDir = function(dir) {
    if (this.options.rememberLastDir) {
    return dir ? this.cookie(this.lCookie, dir) : this.cookie(this.lCookie);
    }
    }

    So does this mean that if I pass the variable dir to the function and if the this.options.rememberLastDir is also true it will return “dir” as the value of the function as well as the cookie, otherwise it will just return the value of the cookie?

    Am I right?

    Comment by Anonymous — December 4, 2011 @ 1:40 pm

  11. Hey there! This is my first visit to your blog!

    We are a collection of volunteers and starting a new project in a community in the same
    niche. Your blog provided us useful information to work on.
    You have done a extraordinary job!

    Comment by Parinita Master — October 5, 2012 @ 11:50 pm

  12. Thanks alot, also helped me out there :)

    Comment by Sidney — January 31, 2013 @ 8:30 am

  13. Hi I am so happy I found your blog, I really found you by error,
    while I was looking on Digg for something else, Anyhow I am here now and would
    just like to say cheers for a marvelous post and a all
    round enjoyable blog (I also love the theme/design), I don’t have time to go through it all at the minute
    but I have saved it and also added in your RSS
    feeds, so when I have time I will be back to read
    a great deal more, Please do keep up the superb b.

    Comment by virgobaker40.xanga.com — May 3, 2013 @ 3:10 am

  14. We’re a gaggle of volunteers and starting a brand new scheme in our community. Your website provided us with useful information to work on. You have performed a formidable job and our whole group might be thankful to you.

    Comment by health insurance — May 7, 2013 @ 8:31 pm

  15. You need to take part in a contest for one of the most useful websites on the web.
    I most certainly will recommend this blog!

    Comment by insurance — May 8, 2013 @ 7:48 am

  16. you are truly a excellent webmaster. The site loading speed is amazing.
    It kind of feels that you are doing any distinctive trick.
    Moreover, The contents are masterwork. you’ve performed a fantastic job on this subject!

    Comment by health insurance — May 13, 2013 @ 1:17 pm

  17. This website was… how do I say it? Relevant!

    ! Finally I have found something which helped me. Many thanks!

    Comment by life insurance — May 19, 2013 @ 8:09 pm

  18. This is a topic which is close to my heart… Cheers!
    Where are your contact details though?

    Comment by insurance — May 19, 2013 @ 8:17 pm

  19. Today, I went to the beach front with my children. I found a sea shell and gave it to my 4 year old
    daughter and said “You can hear the ocean if you put this to your ear.” She put
    the shell to her ear and screamed. There was a hermit crab inside and it pinched her ear.

    She never wants to go back! LoL I know this is entirely
    off topic but I had to tell someone!

    Comment by samuel goldsmith — May 21, 2013 @ 9:55 pm

  20. When I initially commented I clicked the “Notify me when new comments are added” checkbox and
    now each time a comment is added I get three emails with the same comment.
    Is there any way you can remove people from that service?
    Cheers!

    Comment by health insurance — May 23, 2013 @ 8:41 am

  21. Its such as you learn my thoughts! You appear to grasp so much about this,
    like you wrote the book in it or something. I think that you can do with a few % to force the message house a little
    bit, however other than that, that is fantastic
    blog. A great read. I will certainly be back.

    Comment by ranking lekarzy — June 25, 2013 @ 1:25 pm

  22. Hey there, You’ve done an incredible job. I will definitely digg it and personally suggest to my friends. I am sure they will be benefited from this website.

    Comment by Domy z Bali — July 1, 2013 @ 10:45 pm

  23. Thanks for one’s marvelous posting! I actually enjoyed reading it, you’re a great author.I will make certain to bookmark your blog and will come back very soon. I want to encourage you to continue your great job, have a nice day!

    Comment by sdorttuii plmnr — June 17, 2015 @ 7:00 am

  24. It has the that implies that we assign to it and that we uncover for it, not that someone else like a religious chief assigns for us. Most of us find that not debilitating nevertheless empowering. It wasn’t merely the fawning Democrats who’ve been enabling this circus, however there have been many, sadly, many sycophantic Republicans who helped as well,” Malkin stated. First, I’m prepared to confess that I’m a phoney, sycophantic arse-kisser and that I’m extra of one than Mr Johnson (or my husband).

    Comment by wblpvwkdb@gmail.com — July 13, 2015 @ 12:25 am


RSS feed for comments on this post. TrackBack URI

Leave a reply to Giant Geek Blog » Ternary Operators and Assignment Cancel reply

Blog at WordPress.com.