English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JavaScript Basic Tutorial

JavaScript Object

JavaScript Function

JS HTML DOM

JS Browser BOM

AJAX Basic Tutorial

JavaScript Reference Manual

JavaScript Strings (String)

A string is a sequence of one or more characters, which can be composed of letters, numbers, or symbols.

Strings in JavaScript are primitive data types and immutable, which means they are unchangeable.

JavaScript strings

JavaScript strings are zero or more characters enclosed in quotes.

var x = "JavaScript tutorial ";
Test to see‹/›

In JavaScript, you can choose单引号or双引号to enclose strings. Both methods work normally:

var msg1 = "Hello world";   // Use double quotes
var msg2 = 'Hello world';   // Use single quotes
Test to see‹/›

You can use quotes within a string, as long as they do not match the quotes surrounding the string:

var str1 = 'She said \  // Double quotes inside single quotes
var str2 = "She said 'Hey' and left";  // Single quotes inside double quotes
var str3 = "Let's have a cup of tea";  // Single quotes inside double quotes
var str4 = 'We\'ll never give up'; // Use backslashes to escape single quotes
Test to see‹/›

The latest method to create a string is called模板常量(template literal).

Template literals use backticks (`) and work in the same way as regular strings:

var x = `This string uses backticks.`;
Test to see‹/›

Calculate the length of the string

lengthThe property returns the length of the string. For an empty string, the length is 0.

var str = 'JavaScript tutorial ';
str.length;// return 15
Test to see‹/›

Note:Whitespace is also considered a character.

String Concatenation

Concatenation means joining two or more strings together to create a new string.

+ The operator is used to concatenate (join) strings.

var str1 = "quick brown fox";
var str2 = "over the lazy dog";
var str3 = "The " + str1 + " jumps " + str2;
Test to see‹/›

template literalA special feature of functions is that they can include expressions and variables within strings. We can insert variables using the ${} syntax without concatenation.

var str1 = "quick brown fox";
var str2 = "over the lazy dog";
var str3 = `The ${str1} jumps ${str2}.`;
Test to see‹/›

In this case, using template literals may be easier to write and more convenient.

Escape sequences

Because strings must be enclosed in quotes, the following content will cause an error because it will confuse the browser about the end of the string:

var x = 'We'll never give up';
var y = "She said 'Hey' and left";

Escape sequences mean that we operate on them to ensure that they are recognized as text and not part of the code.

In JavaScript, we achieve this by prefixing the character with a backslash ().

Code结果描述
\''单引号
\"""双引号
\\\反斜杠

该序列\' 在字符串中插入一个单引号:

var x = 'We\'ll never give up';
Test to see‹/›

该序列\" 在字符串中插入双引号:

var x = "She said \\
Test to see‹/›

该序列\\ 在字符串中插入反斜杠:

var x = "The character \\ is called backslash";
Test to see‹/›

JavaScript中还有其他六个转义序列有效:

Code描述
\b退格键
\f换页
\n新队
\r回车
\t水平制表符
\v垂直制表符

对于要使用无法使用键盘键入的字符的情况,转义序列也很有用。

长代码换行

为了获得最佳可读性,应避免代码行超过80个字符。

如果JavaScript语句不适合一行,则打破它的最佳位置是在运算符之后:

document.getElementById("para").innerHTML = "The sum of 20 and 30 is " +
sum;
Test to see‹/›

在一行上写一个很长的字符串将很快变得很难阅读和使用。

我们可以使用串联运算符(+)在多行上显示字符串。

var str = "空气污染是化学物质进入 " +
"大气层。它破坏环境平衡并导致 " +
"几种疾病.";
Test to see‹/›

除了使用多个字符串,我们还可以使用\转义符。

var str = "空气污染是化学物质进入 \
大气层。它破坏环境平衡并导致 \
几种疾病.";
Test to see‹/›

Note:所述的(\)方法是不优选的,因为它可能导致某些浏览器和minifiers问题。

为了使代码更具可读性,我们可以改用模板常量(template literal)字符串。这些消除了对长字符串进行串联或转义的需要。

var str = `空气污染是化学物质进入
大气层。它破坏环境平衡并导致
几种疾病.`;
Test to see‹/›

重要的是要知道创建跨多行的字符串的所有方法,因为不同的代码库可能使用各种标准。

字符串基元和字符串对象

通常,JavaScript字符串是从文字创建的原始值:

var city = "New Delhi";

But, you can also use the new keyword to define a string as an object:

var city = new String("New Delhi");

To test the difference between the two, we will initialize a string primitive and a string object.

var str1 = "New Delhi";
var str2 = new String("New Delhi");
typeof str1// returns string
typeof str2// returns object
Test to see‹/›

Note:Do not create strings as objects. This will slow down execution and may produce some unexpected results.

When using the == operator, equal strings are the same:

var str1 = "New Delhi";
var str2 = new String("New Delhi");
document.write(str1 == str2); //returns true because str1and str2are equal
Test to see‹/›

When using the === operator, equal strings are not the same because the === operator expects both value and type to be equal:

var str1 = "New Delhi";
var str2 = new String("New Delhi");
document.write(str1 === str2); //returns false because str1and str2of different types
Test to see‹/›

Cannot compare objects:

var str1 = new String("New Delhi");
var str2 = new String("New Delhi");
document.write(str1 == str2); //because str1and str2They are different objects, so it returns false
document.write(str1 === str2); //because str1and str2They are different objects, so it returns false
Test to see‹/›

Note the difference between (==) and (===). Comparing two JavaScript objects will always return false.