Javascript String Constructor

String is the built-in object corresponding to the primitive string data type. It contains a very large number of methods for string manipulation and examination, substring extraction, and even conversion of strings to marked-up HTML, though unfortunately not standards-oriented XHTML.

The String() constructor takes an optional argument that specifies its initial value:

var s = new String();

var headline = new String("Dewey Defeats Truman");


Because you can invoke String methods on primitive strings, programmers rarely create String objects in practice.

The only property of String is length, which indicates the number of characters in the string.

var s = "String fun in JavaScript";

var strlen = s.length;

// strlen is set to 24


The length property is automatically updated when the string changes and cannot be set by the programmer. In fact there is no way to manipulate a string directly. That is, String methods do not operate on their data “in place.” Any method that would change the value of the string, returns a string containing the result. If you want to change the value of the string, you must set the string equal to the result of the operation. For example, converting a string to uppercase with the toUpperCase() method would require the following syntax:

var s = "abc";

s = s.toUpperCase();

// s is now "ABC"
Invoking s.toUpperCase() without setting s equal to its result does not change the value of s. The following does not modify s:

var s = "abc";

s.toUpperCase();

// s is still "abc"
Other simple string manipulation methods such as toLowerCase() work in the same way; forgetting this fact is a common mistake made by new JavaScript programmers.
Related Posts:
  • JavaScript Security Downloading and running programs written by unknown parties is a dangerous proposition. A program available on the Web could work as advertised, but then again it could also install spyware, a backdoor into your system, … Read More
  • Creating a Cookie-Javascript To make life easier for ourselves, we'll write a function that allows us to create a new cookie and set certain of its attributes with more ease. We'll look at the code first and create an example using it shortly. fun… Read More
  • javascript Date, Time Best seo practices How to promoting webpages Top seo tips top 10 e-commerce tips Additional seo tips Advanced seo tips Mobile seo tips Javascript Date The methods getDate(), getDay(), getMonth(), and g… Read More
  • Javascript Array Creating an Object We have already seen an example of an Array object being created. To create an Array object, we used the JavaScript statement var myArray = new Array(); So how is this statement made up? The first ha… Read More
  • Javascript cookie string We can set a cookie by setting document.cookie to a cookie string.  The following code will set a cookie with the UserName set as Paul,  and an expiration date of 28 December 2010. <html> <head> <… Read More
  • JavaScript Interview Questions JavaScript Interview Questions What is the difference between == and === ? The == checks for value equality, but === checks for both type and value. difference between innerHTML and append() in JavaScript? InnerHTML is… Read More
  • concat() Method The concat() method returns the array resulting from appending its arguments to  the array on which it was invoked. Given the script: var myArray = ["red", "green", "blue"]; alert(myArray.concat("cyan", "yello… Read More
  • WebSocket() constructor socket with the WebSocket() constructor:var s = new WebSocket("ws://ws.example.com/resource");The argument to the WebSocket() constructor is a URL that usesthe ws:// protocol (or wss:// for a secure connection like thatused … Read More
  • JavaScript Timers setTimeout() and setInterval() allow you to register a functionto be invoked once or repeatedly after a specified amountof time has elapsed. These are important global functions ofclient-side JavaScript, and are therefore de… Read More
  • Include Inline JavaScript code To include inline JavaScript code, place JavaScript code inside the <script> element directly, as follows: <script type=”text/javascript”> function sayHi(){ alert(“Hi!”); } </script> When using inline Java… Read More
  • Detecting JavaScript and Cookie Compatibility You may be expecting a huge dump of code to see if JavaScript and cookies are enabled. There ’ s no way that you ’ d want to go through with something like that at this point in the project, so the following minimalist code … Read More
  • PHP and Javascript Variables Variables To define a variable in PHP, you’d write:// PHP$n = 1;The equivalent in JavaScript is:// JavaScriptvar n = 1; There’s no dollar sign, just the name of the variable.  Like in PHP, you don’t define variable… Read More
  • Reserved Words in JavaScript Reserved Words in JavaScript 1.5 >abstract else instanceof switch >boolean enum int synchronized >break export interface this byte extends long throw case f… Read More
  • Retrieving the Response A complete HTTP response consists of a status code, a set ofresponse headers, and a response body. These are availablethrough properties and methods of the XMLHttpRequestobject:• The status and statusText properties return t… Read More
  • Prototypes Every Java-Script object has a second JavaScript object (or null, but thisis rare) associated with it. This second object is known as aprototype, and the first object inherits properties from theprototype.All objects created… Read More