DEV Community

loading...

JavaScript: Strings (Beginners)

banesag profile image Banesa Guaderrama ・2 min read

alt text

Strings are a type of value that can be assigned to variables. A string itself is a collection of characters that can include letters and symbols. A string is a value representing text. Quotes, single or double, are used to create string literals; the quotes are there to indicate that the enclosed text is not code, but a value.

Example:

‘hello’

You should put special attention when naming your variables, and avoid starting a name variable with underscore, since there are private properties and methods named this way, so it can cause an error on your code. Also avoid start your variables with the $ character since it is common to see it in the jQuery library, and it also can cause problems on your code.

Simple tricks to remember and prevent common mistakes for beginners’ coders is the use of quotes; for example, a string literal can be created with single quote marks.

But if you want to use double quotation marks, is also acceptable.

“hello”

Now, if your string contains an apostrophe,

‘It’s me’ // It will cause an error

Then, you need to write as follows:

“it’s me” // this will work because the double quotes are enclosing the string

Another option, is the use of backlash before the apostrophe inside the string:

‘It\’s me’ // the backlash avoids to cause an error in the use of apostrophes

String Properties and Methods

Properties refer to information about the object or its value, and methods are actions performed on the object or value (string). The method to access the properties of a string is by using dot notation (.) followed by the property we are interested in.

An example to find the length property to know how many characters are in a string.

Example:

Const name = ‘banesa’; // Declared and assigned variable
name.length; // this will return the variable’s length
<< 6

Alternative notation method:

Name[‘length’]; // Using square brackets is an alternative notation method
<< 6

Call a method by using dot notation (.); this is by writing the property, then the dot notation followed by the method you want to apply, as follows:

name.toUpperCase();
<< ‘BANESA’

name.toLowerCase();
<< ‘banesa’

name.includes(‘a’);
<< true

name.startsWith(‘b’);
<< true

Template Literals

The main characteristic of template literals is that they use backtick character and it allows the use of double quotes.

`I said, “It’s late!” ` ;

Template literals also allow string interpolation, this is helpful because you can add variables directly into a string by the use of ${} syntax.

Example:

Const name = ‘Banesa’;
`Hello ${ name }!`;
<< ‘Hello Banesa!’

Good practice:

Use const to ensure you are not assigning the same variable in different parts of your code, specially when your code becomes larger and you can forget the already assigned variables.

Use semicolon at the end of each line.

I hope this reading can help you if you are a beginner or aspiring coder, or if you feel that you need to go back to the basics and reinforce them.

Discussion (2)

pic
Editor guide
Collapse
tux0r profile image
tux0r

Quotes, single or double, are used to create string literals

Potentially relevant: JSON will not accept single quotes.

Collapse
banesag profile image
Banesa Guaderrama Author

Hey tuxOr! Absolutely relevant! Thanks for your input! And, this makes another important point for us to ensure that even following the rules or guidelines of one language, it can cause conflict with another library or language rule, there the importance of knowing the different notation methods and be able to detect when to use one or the other, or how to escape a character to avoid/correct conflicts.