JavaScript
  Home arrow JavaScript arrow Page 2 - Stringing Things Along
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
JAVASCRIPT

Stringing Things Along
By: Harish Kamath, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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

    - Introduction to JavaScript
    - Adding Elements to a Tree with TreeView jQue...
    - Using the Persist Argument in a TreeView jQu...
    - Using Unique and Toggle in a TreeView jQuery...
    - Using Event Delegation for Mouseover Events ...
    - Using the Animate Option in a Treeview jQuer...
    - Using HTML Lists with Event Delegation in Ja...
    - Opened and Closed Branches on a TreeView jQu...
    - Mouseover Events and Event Delegation in Jav...
    - Creating a TreeView JQuery Hierarchical Navi...
    - Event Delegation in JavaScript
    - A Look at the New YUI Carousel Control
    - Working with Draggable Elements and Transpar...
    - Displaying Pinned Handles with Resizable Con...
    - Building Resizable Containers with the Ext J...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek