JavaScript
  Home arrow JavaScript arrow Page 2 - Stringing Things Along
Dev Shed Forums 
Administration  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Download TestComplete 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Stringing Things Along
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 10
    2003-01-22

    Table of Contents:
  • Stringing Things Along
  • Elementary School
  • When Size Does Matter
  • Slice And Dice
  • Building Character
  • Search And Destroy
  • Bigger, Bolder, Better

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Dell PowerEdge Servers

    Stringing Things Along - Elementary School
    (Page 2 of 7 )

    Let's start with the basics. In JavaScript, the term "string" refers to a sequence of characters. The following are all valid examples of strings:

    "hey wassup?"
    "I ROCK!"
    "a long time ago in a galaxy far, far away"


    String values can be assigned to a variable using the standard assignment operator.

    <script language="Javascript">
    var myStr = "Elementary, my dear Watson";
    </script>
    


    String values may be enclosed in either double quotes ("") or single quotes('') - the following variable assignments are equivalent:

    <script language="Javascript">
    var myStr = "Elementary, my dear Watson";
    var myStr = 'Elementary, my dear Watson';
    </script>
    


    This comes in handy when you need to embed HTML code in your JavaScript strings, and don't want the various sets of quotes to conflict with each other. Consider the following example, which illustrates:

    <script language="Javascript">
    // bad
    var myStr = "<a href="http://www.melonfire.com">Home</a>";
    // good
    var myStr = '<a href="http://www.melonfire.com">Home</a>';
    </script>
    


    You can also take the scenic route, and define a string using JavaScript's built-in String object, as below:

    <script language="Javascript">
    var myStr = "Elementary, my dear Watson";
    // this is equivalent
    var myStr = new String();
    myStr = "Elementary, my dear Watson";
    // and so is this
    var myStr = new String("Elementary, my dear Watson");
    </script>
    


    When you do this, you create a proper JavaScript object, and thereby have access to all the methods and properties of that object. Note that it's not essential to create strings using the second technique outlined above; it's far more common (not to mention easier on your keyboard) to use the first one, because the JavaScript interpreter automatically converts string datatypes into string objects whenever necessary.

    If you're a JavaScript newbie, this next example won't do much for you - but for those of you who are purists, this next code snippet will help in making the distinction between a datatype and an object clearer.

    <script language="Javascript">
    var myStr = "Hit me";
    // this returns "string"
    alert(typeof(myStr));
    var myStr = new String("Hit me");
    // this returns "object"
    alert(typeof(myStr));
    </script>
    


    You can concatenate strings with the addition (+) operator - as the following example demonstrates:

    <script language="Javascript">
    var fname = "James";
    var lname = "Bond";
    // concatenate the two, separate with a space
    var name = fname + " " + lname;
    </script>
    


    If your string contains quotes, carriage returns or backslashes, it's necessary to escape these special characters with a backslash.

    <script language="Javascript">
    // will cause an error due to mismatched quotes
    var movie = 'Baby's Day Out';
    // will be fine
    var movie = 'Baby\'s Day Out';
    </script>
    


    Try it out yourself and see, and then flip the page to see how to dress up your strings.

    More JavaScript Articles
    More By Harish Kamath, (c) Melonfire


     

       

    JAVASCRIPT ARTICLES

    - Getting Attention with Interactive Effects
    - Interacting with Tooltips and Previews
    - Just-in-Time Information and Ajax
    - Interactive Effects
    - Using Cookies With JavaScript
    - Understanding the JavaScript RegExp Object
    - Controlling Browser Properties with JavaScri...
    - Using Timers in JavaScript
    - Form Validation with JavaScript
    - JavaScript Exception Handling
    - Stringing Things Along
    - Understanding The JavaScript Event Model (pa...
    - Understanding The JavaScript Event Model (pa...
    - An Object Lesson In JavaScript

     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway