JavaScript strings share many similarities with string implementations from other high-level languages. They represent text based messages and data.
In this course we will cover the basics. How to create new strings and perform common operations on them.
Here is an example of a string:
"Hello World"
You can define strings in JavaScript by enclosing the text in single quotes or double quotes:
// Single quotes can be used
var str = 'Our lovely string';
// Double quotes as well
var otherStr = "Another nice string";
In Javascript, Strings can contain UTF-8 characters:
"中文 español English हिन्दी العربية português বাংলা русский 日本語 ਪੰਜਾਬੀ 한국어";
Note: Strings can not be subtracted, multiplied or divided.
Concatenation involves adding two or more strings together, creating a larger string containing the combined data of those original strings. This is done in JavaScript using the + operator.
var bigStr = 'Hi ' + 'JS strings are nice ' + 'and ' + 'easy to add';
It's easy in Javascript to know how many characters are in string using the property .length
.
// Just use the property .length
var size = 'Our lovely string'.length;
Note: Strings can not be substracted, multiplied or divided.