Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.

We know that JavaScript is an ‘object-oriented’ programming language, which contains objects such as document document objects, String objects, Array Array objects, window objects, and Form objects. Using JavaScript can enrich our programs and make them more vivid.

Today we have learned the common methods and common attributes of string, here is the summary:

String object String

JavaScript uses String to represent text, and String is a basic data type in JavaScript.

A string is an ordered sequence of 16 – bit values that cannot be modified, so it has length.

String object literal

String in JavaScript, with matching pairs of single, double, or backquotes; Single and double quotation marks can contain each other

' ' // An empty string
'juejin.cn' //
'52.3'
'30.37'
'47.9'
'utm_source="gold_browser_extension"'
"utm_source='gold_browser_extension'"
Rare-earth Nuggets a community that helps developers grow
"Become a Better Developer(s's) by Sharing and Learning,"
Copy the code

In ES5 strings can be written with a \ line break at the end of the string, but will still be parsed on one line; Use the \n newline character to indicate a newline

// Three lines still represent one line:
// Indicates the result: this is a longer text.

'This is a longer word.'

// The following string is written on one line but represents two lines \n represents a newline character
'two\nlines'

Copy the code

The ‘rare earth nuggets’ backquotes above (”) are ES6 syntax and support line breaks. In single/double quotation marks, note that newlines conform to Spaces

Common properties of the String object String

  1. Length string length attribute.

  2. Prototype: Adds attributes to string objects

  3. Constructor: Represents the function model of a character object

Declare a string object, firstString, and print the number of characters in it

const firstString = 'Today we learned about the common methods of strings and their common properties'

document.write(firstString.length) // The length of the characters in the output string object: 19
Copy the code