Loading Please Wait...

Logo Lynxsia IT Solutions

JavaScript Strings

JS Strings

JavaScript strings are for storing and manipulating text.

A JavaScript string is zero or more characters written inside quotes (single or double). You can even use quotes inside string if they do not match the enclosing quotes.

					 
        
          let text1 = "Lynxsia IT Solutions";
          let text2 = 'Lynxsia IT Solutions';
          let text3 = "'Lynxsia' IT Solutions";
          let text4 = '"Lynxsia" IT Solutions';
        
      
Character Escape

As JavaScript uses quotes to write strings, any apostrophe, slashes, etc will mislead the JavaScript engine to chop (cut) the string and generate errors.

					 
        
          let text1 = "We are the "Lynxsia" IT Solutions"; // error
          let text2 = 'It's a good day'; // error
        
      
Escape Sequences
Code Result Description
\' ' Single Quotes
\" " Double Quotes
\\ \ Backslash
\b   Backspace
\f   Form Feed
\n   New Line
\r   Carriage Return
\t   Horizontal Tabulator
\v   Vertical Tabulator
					 
        
          let text1 = "We are the \"Lynxsia\" IT Solutions";
          let text2 = 'It\'s a good day';
        
      
The escape characters \b, \f, \n, \r, \t, \v were originally designed to control typewriters, teletypes, and fax machines. They do not make any sense in HTML.
Breaking Long Code Lines

For best readability, programmers often like to avoid code lines longer than 80 characters.

If a JavaScript statement does not fit on one line, the best place to break it is after an operator.

You can also break up a code line within a text string with a single backslash. The \ method is not the preferred method. It might not have universal support. Some browsers do not allow spaces behind the \ character.

A safer way to break up a string, is to use string addition

					 
        
          // after operator
          let text1 = 
          "We are the Lynxsia IT Solutions";

          // using backslash
          let text2 = "We are the \ 
          Lynxsia IT Solutions";

          // using string addition
          let text3 = "We are the " +
          "Lynxsia IT Solutions";
        
      
JavaScript Strings as Objects

Strings can also be defined as objects with the keyword new.

					 
        
          let a = new String("Lynxsia");
        
      
Do not create Number objects. The new keyword complicates the code and slows down execution speed. Number Objects can produce unexpected results. Comparing two JavaScript objects always returns false.
					 
        
          let a = "Lynxsia";
          let b = new String("Lynxsia");
          let c = new String("Lynxsia");

          (a == b)  // returns true
          (a === b)  // returns false
          (b == c)  // returns false
          (b === c)  // returns false
        
      

How you feel about this blog:

Share this blog on:

Report Us

If you find any error in the turtorials, or want to share your suggestion/feedback, feel free to send us email at: info@lynxsia.com

Contact Us
Ads
Logo
Lynxsia IT Solutions

We are concern with various development process like website design & development, E-commerce development, Software development, Application development, SMS & Bulk SMS Provider, PWA Development, and many more..

Kotwali Road, Chhiptehri, Banda, 210001, UP, India

Copyright © 2022, Lynxsia IT Solutions, All rights reserved