DEV Community

Banesa Guaderrama
Banesa Guaderrama

Posted on

JavaScript: Strings (Beginners)

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’
Enter fullscreen mode Exit fullscreen mode

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”
Enter fullscreen mode Exit fullscreen mode

Now, if your string contains an apostrophe,

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

Enter fullscreen mode Exit fullscreen mode

Then, you need to write as follows:

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

Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Alternative notation method:

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

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
Enter fullscreen mode Exit fullscreen mode

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!” ` ;
Enter fullscreen mode Exit fullscreen mode

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!’
Enter fullscreen mode Exit fullscreen mode

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.

Top comments (3)

Collapse
 
banesag profile image
Banesa Guaderrama

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.

Collapse
 
kaykaycodes profile image
Shanakay Hall

thank you for this!