www.webdeveloper.com
Results 1 to 9 of 9

Thread: JavaScript-Special characters

  1. #1
    Join Date
    Jun 2011
    Posts
    23

    JavaScript-Special characters

    I recently found out what the ? sign means in javascript.. is there anything else like that to shorten code?

    Thanks!

  2. #2
    Join Date
    Feb 2006
    Posts
    2,927
    The delete key is the best javascript optimizer.

  3. #3
    Join Date
    Jun 2011
    Posts
    23
    I'm not uploading any site to the internet, so the spaces don't bother me too much right now... This is basically out of curiosity, and not trying to reduce load time.

  4. #4
    Join Date
    Dec 2002
    Location
    St. Louis, MO, USA
    Posts
    1,582
    Quote Originally Posted by AJAXnub View Post
    I recently found out what the ? sign means in javascript.. is there anything else like that to shorten code?

    Thanks!
    What.. that the ? is used in ternery operations between the condition and the true/false commands?

    Well.. off the top of my head, the only other shortcut I can think of is +=.

    a = a + b concatenation can be shortened to a += b. Not sure if it works for math.

    When incrementing a number, instead of i=i+1, you can use i++; same thing for subtracting, i--.

  5. #5
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    You can write compounded statements, e.g. +=, -=, /=, *=, etc.
    You can use the ternary operator (C?T:F).
    You can take out lines of code by combining statements together when possible, usually done with string parsing.
    You can cut out variables that are only used for storage for a single get operation.
    You can eliminate loops and function calls for single statements.

  6. #6
    Join Date
    Jun 2011
    Posts
    23
    hmm.. can you explain the last three? I'm not very good with terminology...

  7. #7
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    PHP Code:
    //instead of
    var str "test";
    str += " test2";
    document.write(str);
    //use
    document.write("test test2"); 
    PHP Code:
    //instead of
    function output_text(text) {
    document.getElementById('someplace').innerHTML text;
    }
    //and somewhere else
    output_text("test");
    //just use
    document.getElementById("someplace").innerHTML "test";
    //or have a global variable for "someplace" and use
    document.getElementById(someplace_id).innerHTML text

  8. #8
    Join Date
    Jun 2011
    Posts
    23
    o.O I didn't know doing that was bad.. Thanks!

  9. #9
    Join Date
    Oct 2007
    Location
    Western Massachusetts, USA
    Posts
    387
    it just adds in some extra overhead when calling functions or adding statements. Not very noticible, but it creates speed where maintainability was.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles