AgerNic.com
WEB DEVELOPER SITE, HTML, CSS, PHP, SQL

JavaScript String


JavaScript Tutorial » JavaScript String

A string are used for storing and manipulating a sequence of letters, numbers, special characters and arithmetic values or combination of all

Strings can be created by enclosing the string within single quotes (') or double quotes (").

Example:
<script>
var my_String = 'Have a good day'; // Single quoted
var my_String = "Have a good day"; // Double quoted
</script>

You can use quotes inside a string, as long as they don't match the quotes surrounding the string:

Example:
<script>
var my_str1 = "it's done";
var my_str2 = 'He said "Have a good day"';
var my_str3 = "She replied 'Have a good day to you too'";
var my_str4 = 'Have a good day"; // Syntax error - quotes must match
</script>

You can use single quotes inside a single quoted strings and double quotes inside double quoted strings as log as the quotes is preceded with a backslash character (\), like this: :

Example: have two methods that add numbers of different type:
<script>
var my_str1 = 'it\'s okay';
var my_str2 = "He said \"Goodbye\"";
var my_str3 = 'She replied \'Calm down, please\'';
</script>
Output:


JavaScript Escape Sequences- example


Most commonly used escape sequences>

1. \n is replaced by the newline character
2. \t is replaced by the tab character
3. \r is replaced by the carriage-return character
4. \b is replaced by the backspace character
5. \\ is replaced by a single backslash (\)

Example:
<script>
// Creating variables
var my_str1 = "You will learn programming \n fundamentals and basic \n object-oriented concepts";
document.write("
" + my_str1 + "
"); // Create line break

var my_str2 = "C:\Programs\Downloads";
document.write(my_str2 + "
"); // Prints C:ProgramsDownloads
var my_str3 = "C:\\Programs\\Downloads";
document.write(my_str3); // Prints C:\Programs\Downloads
</script>


Finding a String Inside Another String - example


You can use the indexOf() method to find a substring or string within another string.

Example:

<script>
var my_str = "Here we learn JavaScript starting from scratch";
var pos = my_str.indexOf("learn");
document.write(pos); // 0utputs: 8
</script>

 

JavaScript String contains, to int, replace, interpolation, length, split, methods, to json, concatenation, includes.



contains, to int, replace, interpolation, length, split, methods, to json, concatenation, includes.
JavaScript String - javascript tutorial

Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.


0
Online Editor
ONLINE EDITOR

news templates


COLOR PICKER

news templates
This tool makes it easy to create, adjust, and experiment with custom colors for the web.


HTML Templates
news templates
Magnews2 is a modern and creative free magazine and news website template that will help you kick off your online project in style.


CSS HTML Layout
news templates
Find here examples of creative and unique website layouts.


Free CSS HTML Menu
news templates
Find here examples of creative and unique website CSS HTML menu.