String

Concatenating strings with plus operator:

In JavaScript, when the + operator is used with a String value, it is called the concatenation operator. You can build a new string out of other strings by concatenating them together.

Example

'My name is Alan,' + ' I concatenate.'

Constructing strings with variables:

Sometimes you will need to build a string. By using the concatenation operator (+), you can insert one or more variables into a string you're building.

Example

var ourName = "freeCodeCamp";

var ourStr = "Hello, our name is " + ourName + ", how are you?";


Appending variables to strings:

Just as we can build a string over multiple lines out of string literals, we can also append variables to a string using the plus equals (+=) operator.

Example:

var anAdjective = "awesome!";
var ourStr = "freeCodeCamp is ";
ourStr += anAdjective;


Find the length of a string:

You can find the length of a String value by writing .length after the string variable or string literal.

console.log("Alan Peter".length);

The value 10 would be displayed in the console.

For example, if we created a variable var firstName = "Charles", we could find out how long the string Charles is by using the firstName.length property.



Use bracket notation to find the first character in a string:

Bracket Notation is a way to get a character at a specific index within a string.

The character at index 0 in the word Charles is C. So if var firstName = "Charles", you can get the value of the first letter of the string by using firstName[0].

Example:

var firstName = "Charles";

var firstLetter = firstName[0];

The first letter would have a value of the string C.


Understand string immutability:

In JavaScript, String values are immutable, which means that they cannot be altered once created. For example, the following code:

var myStr = "Bob";
myStr[0] = "J";

Cannot change the value of myStr to Job, because the contents of myStr cannot be altered. Note that this does not mean that myStr cannot be changed, just that the individual characters of a string literal cannot be changed.

The only way to change myStr would be to assign it with a new string, like this:

var myStr = "Bob"; myStr = "Job";


Use bracket notation to find the last character in a string:

In order to get the last letter of a string, you can subtract one from the string's length.

For example, if var firstName = "Charles", you can get the value of the last letter of the string by using firstName[firstName.length - 1].

Example:

var firstName = "Charles";
var lastLetter = firstName[firstName.length - 1];

The lastLetter variable would have a value of the string s.


Use bracket notation to find the nth-to-last character in a string:

You can use the same principle we just used to retrieve the last character in a string to retrieve the Nth-to-last character.

For example, you can get the value of the third-to-last letter of the var firstName = "Charles" string by using firstName[firstName.length - 3]

Example:

var firstName = "Charles";
var thirdToLastLetter = firstName[firstName.length - 3];

thirdToLastLetter would have a value of the string l.


Post a Comment

Previous Post Next Post