DONATE  
MENU
PHP
AJAX
JAVASCRIPT
HTML
CSS
JSON
MYSQL

JavaScript Tutorials

INTRODUCTION

» Introduction to JavaScript

» Practice IT Editor

» JavaScript Example

» JavaScript Placement

» JavaScript Syntax

» JavaScript Statements

» JavaScript Input

» JavaScript Output

» JavaScript Comments

» JavaScript Variables

» Variables' Scope

» JavaScript Operators

» JS Expressions & Concatenation

» JavaScript Data Type

» JavaScript Numbers

» Number Object

» JavaScript Strings

» String Object

» JavaScript Booleans

» Boolean Object

» JavaScript Functions

» JavaScript Arrays

» Array Object - Properties

» Array Object - Methods

» JavaScript Objects

» JavaScript Undefined

» JavaScript Nulls

CONDITIONAL TESTING

» If Statement

» If Else Statement

» JavaScript Ternary Operator

» Switch Statement

LOOPS

» For Loops

» While Loops

» Break & Continue

» For...In Loops

» do...while Loop

FUNCTIONS

» JavaScript Functions

» Creating Functions

» Functions' Parameters

» Functions' Arguments

» Function Invocation

» The Return Keyword

» The Arguments Object

» Callback Functions

» Arrow Functions

ARRAYS

» JavaScript Arrays

» Creating Arrays

» Accessing Arrays

» Looping Arrays

» Multi - dimensional Arrays

OBJECTS

» JavaScript Objects

» Creating Objects

» Prototype Definition

» Accessing Objects

» The this Keyword

AJAX

» Introduction to AJAX

JSON

» Introduction to JSON

JAVASCRIPT DATES

» The Date Object

» Date Methods

» Timing (SetInterval & SetTimeout)

MATH OBJECT

» Math Object

» Math Methods

HTML / JS DOM

» DOM Introduction

» Accessing HTML Elements in DOM

» Get And Change Elements' Contents & Values

» Change Styling - DOM CSS

» DOM Attributes

» JavaScript Event Listeners

» DOM Event Listeners

» JS Events

MISCELLANEOUS

» JS Strict Mode

» JavaScript Hoisting

» JavaScript let keyword

» Reserved Keywords

» JS Best Practices

» JavaScript vs ECMAScript

JAVASCRIPT EXAMPLES

» JavaScript Calculator

.. » JavaScript » Introduction » JavaScript Strings
Share Tweet WhatsApp

JavaScript Strings

A string in JavaScript is any series of characters contained in single(' ') or double(" ") quotes. If any data type is contained in single or double quotes, it automatically becomes a string.
"Hello World";
"124";
'true';
'[1, 3, 4]';
"{name: 'Omamode'}";
"function myFunc(){}";
Above are various strings. Strings can easily be created explicitly using single or double quotes.
var str = ""; //An empty string
var helloText = "Hello World";
var msg = 'sent';
You can also create strings implicitly or using the String() object prototype.
<script>
var helloText = new String("Hello World");

alert(helloText);
</script>

Copy PRACTICE IT

Creation of strings with single/double quotes or the new String() object does same thing. It is therefore recommended that you create strings with single/double quotes. Such strings are easier to create and read. Escaping Special Characters When you create a string with double quotes, it is expected that whatever quotes that should be inside should be single quotes and vice versa. Having double quotes inside a string created with double quotes produces unexpected result and may lead to non execution of your code.
var str = "Owner"s wealth";//WRONG
str = "Owner's wealth"; //RIGHT
str = 'My father's properties'; //WRONG
str = 'My father"s properties'; //RIGHT
If you must have a double quote(s) in a string created with a double quote or single quote(s) in single quote string, you must then escape such quotes using a special character. This is done using a backward slash(\) before the single or double quote. Don't worry, the slash won't form part of your string.
var str = "Owner\"s wealth";//RIGHT
str = 'My father\'s properties';//RIGHT
<script>
alert('Programmers\' tutorials');
</script>

Copy PRACTICE IT

Other Special Characters Are:
Character(s)DESCRIPTION
\nInserts a line break in a string.
<script>
alert("Hello\nWorld");
</script>

Copy PRACTICE IT

\\Enables you to escape the backslash and have a backslash in a string.
<script>
alert("True\\False");
</script>

Copy PRACTICE IT

Some others which were originally designed for typewriters, fax and similar machines and makes no meaning in today JS strings are:
Character(s)DESCRIPTION
\bBackspace.
\fForm feed.
\rCarriage return.
\tHorizontal tabulator.
\vVertical tabulator.
String Concatenation String concatenation is when we join one or more strings with other variables to form another longer string. We used the addition operator (+) to concatenate strings.
var sum = 5 + 2;
var str = "The answer is "+sum; //it becomes "The answer is 7"

var web_name = "StackBeginner";
var url = "stackbeginner.com";
alert("You can visit the "+web_name+" site at "+url+"."); //alerts "You can visit the StackBeginner site at stackbeginner.com."
Convert any data type to string Unlike most data types, any other data type can be converted to a string in JavaScript. A number, boolean, null, undefined, array, object, date and function can be converted to a string. To do this, you can make use of the String() function and the JSON.stringify() method.
<script>
var obj = {name: "stackbeginner", url: "stackbeginner.com", type: "Tutorial website"};

alert(obj); //outputs [object Object] because it is an object

obj = JSON.stringify(obj); //it is now a string
alert(obj);
</script>

Copy PRACTICE IT

The JSON.stringify() method works best when converting objects to strings while the String() function fully supports all other data type to string conversions. String's typeof() When you use the typeof() operator on a string or a variable containing a string, it returns string. Therefore, to test if a value is a string or not, use the typeof() operator on the value.
<script>
alert(typeof("Hello World"));
</script>

Copy PRACTICE IT

You will learn more about the string object properties and methods in the next chapter. SUMMARY
  1. A string is any sequence of characters contained in single or double quotes.
  2. Strings can be created explicitly e.g "Hello World" or implicitly e.g new String("Hello World").
  3. We majorly use escape characters to escape special characters such as single quotes, double quotes, backslashes and to insert line breaks in a string.
  4. String concatenation is joining strings with variables using the addition operator (+).
  5. The String() function and the stringify() method of the JSON object enables you to convert any data type to string.
  6. The typeof operator when used on strings will return string.
Take a short quiz on JavaScript Strings.
Ask a question or tell us if JavaScript Strings is easy or not.
cent?

Join our 1,090 subscribers to receive tutorials' updates!

  We won't spam your mail box. We hate it.





StackBeginner 2021 © Joseph Omamode