0% completed
Strings in JavaScript are used to store and manipulate text. They can be anything from letters
and spaces
to symbols
and numbers
, enclosed within quotes. Understanding how to work with strings is fundamental in JavaScript as they are one of the most common types of data you'll encounter and manipulate in web development.
Strings can be created using single quotes
, double quotes
, or backticks
(template literals), each serving different purposes and functionalities.
Single quotes create a straightforward string.
Double quotes also denote strings and work similarly to single quotes.
Backticks denote template literals, allowing for multi-line strings and string interpolation.
In JavaScript, individual characters within a string can be accessed using the bracket notation ([]
), similar to accessing array elements. This is a common task when you need to inspect, manipulate, or evaluate specific characters within a string.
Let's consider a simple example where we access specific characters in a string:
In this example:
greeting
contains the text "Hello, World!"
.greeting[0]
accesses the first character of the string, which is "H"
, because string indices start at 0.greeting[7]
accesses the eighth character, which is "W"
, demonstrating how you can jump to any position within the string.0
, the second at index 1
, and so on.greeting[50]
, the result will be undefined
.Mixing quotes can be useful for including quotations within a string.
Escape characters in JavaScript are special characters used within strings to represent characters that otherwise have a different meaning or are not easily typed directly into a string. Here's a table of common escape characters:
Escape Character | Represents |
---|---|
\' | Single quote (' ) |
\" | Double quote (" ) |
\\ | Backslash (\ ) |
\n | New line |
\r | Carriage return |
\t | Horizontal tab |
\b | Backspace |
\f | Form feed |
\v | Vertical tab (not supported in all browsers) |
\0 | Null character |
\uXXXX | Unicode character specified by hexadecimal number XXXX |
These escape characters enable you to include special characters in strings, format strings in specific ways (e.g., adding new lines or tabs), and use characters that would otherwise conflict with the string delimiters.
Before ES6, strings spanned multiple lines by using the escape character. Template literals introduced a cleaner way to write multiline strings.
When you add a number and a string together, JavaScript converts the number to a string.
In JavaScript, strings can be defined as both primitives and objects. While it's more common to work with string literals (primitives), understanding how and why strings might be used as objects can be beneficial.
String objects are created using the new
keyword along with the String
constructor. This approach wraps the string literal in an object:
stringObject
is an instance of the String object. It behaves differently from a string primitive in that it's a distinct object with properties and methods.Let's compare a String object with a string literal:
typeof
operator reveals that stringObject
is an "object", whereas stringLiteral
is a "string". This difference illustrates the fundamental distinction between String objects and string literals.stringObject
and stringLiteral
using the strict equality operator (===
), the result is false
because one is an object and the other is a primitive value.==
or ===
are compared by their values, while String objects are compared by reference, meaning two String objects with the same value are not strictly equal because they are different objects in memory......
.....
.....