(JavaScript Tutorials for Beginners)
In this end-to-end example, you will learn – JavaScript tutorials for Beginners – JavaScript String.
JavaScript String
In this tutorial, you will learn about JavaScript string with the help of examples.
JavaScript string is a primitive data type that is used to work with texts. For example,
let name = 'John';
Create JavaScript Strings
In JavaScript, strings are created by surrounding them with quotes. There are three ways you can use quotes.
- Single quotes:
'Hello'
- Double quotes:
"Hello"
- Backticks:
`Hello`
For example,
//strings example
let name = 'Peter';
let name1 = "Jack";
let result = `The names are ${name} and ${name1}`;
Single quotes and double quotes are practically the same and you can use either of them.
Backticks are generally used when you need to include variables or expressions into a string. This is done by wrapping variables or expressions with ${variable or expression}
as shown above.
You can also write a quote inside another quote. For example,
let name = 'My name is "Peter".';
However, the quote should not match the surrounding quotes. For example,
let name = 'My name is 'Peter'.'; // error
Access String Characters
You can access the characters in a string in two ways.
- One way is to treat strings as an array. For example,
let a = 'hello';
console.log(a[1]); // "e"
- Another way is to use the method
charAt()
. For example,
let a = 'hello';
console.log(a.charAt(1)); // "e"
JavaScript Strings are immutable
In JavaScript, strings are immutable. That means the characters of a string cannot be changed. For example,
let a = 'hello';
a[0] = 'H';
console.log(a); // "hello"
However, you can assign the variable name to a new string. For example,
let a = 'hello';
a = 'Hello';
console.log(a); // "Hello"
JavaScript is Case-Sensitive
JavaScript is case-sensitive. That means in JavaScript, the lowercase and uppercase letters are treated as different values. For example,
let a = 'a';
let b = 'A'
console.log(a === b); // false
In JavaScript, a
and A
are treated as different values.
JavaScript Multiline Strings
To use a multiline string, you can either use the +
operator or the operator. For example,
// using the + operator
let message1 = 'This is a long message ' +
'that spans across multiple lines' +
'in the code.'
// using the operator
let message2 = 'This is a long message
that spans across multiple lines
in the code.'
JavaScript String Length
To find the length of a string, you can use built-in length
property. For example,
let a = 'hello';
console.log(a.length); // 5
JavaScript String Objects
You can also create strings using the new
keyword. For example,
let a = 'hello';
let b = new String('hello');
console.log(a); // "hello"
console.log(b); // "hello"
console.log(typeof a); // "string"
console.log(typeof b); // "object"
Note: It is recommended to avoid using string objects. Using string objects slows down the program.
JavaScript String Methods
Here are the commonly used JavaScript String methods:
Method | Description |
---|---|
charAt(index) | returns the character at specified index |
concat() | joins two or more strings |
replace() | replaces a string with another string |
split() | converts the string to an array of strings |
substr(start, length) | returns a part of a string |
substring(start,end) | returns a part of a string |
slice(start, end) | returns a part of a string |
toLowerCase() | returns the passed string in lower case |
toUpperCase() | returns the passed string in upper case |
trim() | removes whitespace from the strings |
includes() | searches for a string and returns a boolean value |
search() | searches for a string and returns a position of a match |
Example: JavaScript String Methods
let text1 = 'hello';
let text2 = 'world';
let text3 = ' JavaScript ';
// concatenating two strings
let result1 = text1.concat(' ', text2);
console.log(result1); // "hello world"
// converting the text to uppercase
let result2 = text1.toUpperCase();
console.log(result2); // HELLO
// removing whitespace from the string
let result3 = text3.trim();
console.log(result3); // JavaScript
// converting the string to an array
let result4 = text1.split();
console.log(result4); // ["hello"]
// slicing the string
let result5= text1.slice(1, 3);
console.log(result5); // "el"
JavaScript String() Function
The String()
function is used to convert various data types to strings. For example,
let a = 225; // string
let b = true; // boolean
//converting to number
let result1 = String(a);
let result2 = String(b);
console.log(result1); // "225"
console.log(result2); // "true"
If you want to learn more about the string conversion, visit JavaScript Type Conversion.
Escape Character
You can use the backslash escape character to include special characters in a string. For example,
let name = 'My name is 'Peter'.';
console.log(name);
Output
My name is 'Peter'.
In the above program, the same quote is included using .
Here are the other ways that you can use :
Code | Output |
---|---|
“ | include double quote |
\ | include backslash |
n | new line |
r | carriage return |
v | vertical tab |
t | horizontal tab |
b | backspace |
f | form feed |
Disclaimer: The information and code presented within this recipe/tutorial is only for educational and coaching purposes for beginners and developers. Anyone can practice and apply the recipe/tutorial presented here, but the reader is taking full responsibility for his/her actions. The author (content curator) of this recipe (code / program) has made every effort to ensure the accuracy of the information was correct at time of publication. The author (content curator) does not assume and hereby disclaims any liability to any party for any loss, damage, or disruption caused by errors or omissions, whether such errors or omissions result from accident, negligence, or any other cause. The information presented here could also be found in public knowledge domains.
Learn by Coding: v-Tutorials on Applied Machine Learning and Data Science for Beginners
Latest end-to-end Learn by Coding Projects (Jupyter Notebooks) in Python and R:
All Notebooks in One Bundle: Data Science Recipes and Examples in Python & R.
End-to-End Python Machine Learning Recipes & Examples.
End-to-End R Machine Learning Recipes & Examples.
Applied Statistics with R for Beginners and Business Professionals
Data Science and Machine Learning Projects in Python: Tabular Data Analytics
Data Science and Machine Learning Projects in R: Tabular Data Analytics
Python Machine Learning & Data Science Recipes: Learn by Coding
R Machine Learning & Data Science Recipes: Learn by Coding
Comparing Different Machine Learning Algorithms in Python for Classification (FREE)
There are 2000+ End-to-End Python & R Notebooks are available to build Professional Portfolio as a Data Scientist and/or Machine Learning Specialist. All Notebooks are only $29.95. We would like to request you to have a look at the website for FREE the end-to-end notebooks, and then decide whether you would like to purchase or not.