GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
GeezerVison is showing www.tutorialspoint.com — JavaScript - Strings
Selected Reading
JavaScript - StringsThe String object in JavaScript lets you work with a series of characters; it wraps JavaScript's string primitive data type with a number of helper methods. As JavaScript automatically converts between string primitives and String objects, you can call any of the helper methods of the String object on a string primitive. The string is a sequence of characters containing 0 or more characters. For example, 'Hello' is a string. SyntaxJavaScript strings can be created as objects using the String() constructor or as primitives using string literals. Use the following syntax to create a String object − var val = new String(value); The String parameter, value is a series of characters that has been properly encoded. We can create string primitives using string literals and the String() function as follows − str1 = 'Hello World!'; // using single quote
str2 = "Hello World!"; // using double quote
str3 = 'Hello World'; // using back ticks
str4 = String('Hello World!'); // using String() function
JavaScript String Object PropertiesHere is a list of the properties of String object and their description.
JavaScript String Object MethodsHere is a list of the methods available in String object along with their description. Static MethodsThe static methods are invoked using the 'String' class itself.
Instance MethodsThe instance methods are invoked using the instance of the String class.
String constructor
ExamplesLet's take understand the JavaScript String object and string primitives with the help of some examples. Example: Creating JavaScript String ObjectsIn the example below, we used the string() constructor with the 'new' keyword to create a string object. <html>
<head>
<title> JavaScript - String Object </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
const str = new String("Hello World!");
output.innerHTML += "str == " + str + "<br>";
output.innerHTML += "typeof str == " + typeof str;
</script>
</body>
</html>
Outputstr == Hello World! typeof str == object Accessing a stringYou can access the string characters using its index. The string index starts from 0. ExampleIn the example below, we access the character from the 0th and 4th index of the string. <html>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
let str1 = new String("Welcome!");
let str2 = "Welcome!";
output.innerHTML += "0th character is - " + str1[0] + "<br>";
output.innerHTML += "4th character is - " + str2[4] + "<br>";
</script>
</body>
</html>
Output0th character is - W 4th character is - o JavaScript string is case-sensitiveIn JavaScript, strings are case-sensitive. It means lowercase and uppercase characters are different. ExampleIn the example below, char1 contains the uppercase 'S', and char2 contains the lowercase 's' characters. When you compare char1 and char2, it returns false as strings are case-sensitive. <html>
<head>
<title> JavaScript - String case-sensitivity </title>
</head>
<body>
<p id = "output"></p>
<script>
let char1 = 'S';
let char2 = 's'
let ans = char1 == char2;
document.getElementById("output").innerHTML += "s == S " + ans;
</script>
</body>
</html>
Outputs == S false JavaScript Strings Are ImmutableIn JavaScript, you can't change the characters of the string. However, you can update the whole string. ExampleIn the example below, we try to update the first character of the string, but it doesn't get updated, which you can see in the output. After that, we update the whole string, and you can observe the changes in the string. <html>
<head>
<title> JavaScript − Immutable String </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
let str = "Animal";
str[0] = 'j';
output.innerHTML += "The string is: " + str + "<br>";
str = "Hi!";
output.innerHTML += "The updated string is: " + str;
</script>
</body>
</html>
OutputThe string is: Animal The updated string is: Hi! Escape CharactersYou can use the special characters with the string using the backslash (\) characters. Here is the list of special characters.
ExampleIn the example below, we added a single quote between the characters of the str1 string and a backslash between the characters of the str2 string. <html>
<head>
<title> JavaScript - Escape Characters </title>
</head>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
let str1 = "Your\'s welcome!";
let str2 = "Backslash \\";
output.innerHTML += "str1 == " + str1 + "<br>";
output.innerHTML += "str2 == " + str2 + "<br>";
</script>
</body>
</html>
Outputstr1 == Your's welcome! str2 == Backslash \ String HTML WrappersHere is a list of the methods that return a copy of the string wrapped inside an appropriate HTML tag.
In the following sections, we will have a few examples to demonstrate the usage of String methods. Advertisements
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |