DEV Community

Code_Regina
Code_Regina

Posted on • Edited on

|JavaScript| JavaScript: Strings and More

          -Introducing Strings
          -Indices and Length
          -String Methods
          -String Methods with Arguments
          -String Template Literals
          -Undefined and Null
Enter fullscreen mode Exit fullscreen mode

Introducing Strings

Strings are another primitive type in JavaScript. They represent text, and must be wrapped in quotes.
"String of Characters"


let firstname = "Tim";

let msg = "Please do not feed the animals";

let animal = 'doggo';

let bad = "this is bad";

Enter fullscreen mode Exit fullscreen mode

It is possible to use either single or double quotes.

Indices and Length

Strings are indexed meaning that each character has a corresponding number attached to it.
Chicken
C = 0
h = 1
i = 2
c = 3
k = 4
e = 5
n = 6

String Methods

Methods are built-in actions we can perform with individual strings.
Methods help us do things like searching within a string, replacing part of a string, and changing the casing of a string.

method syntax

thing.method()
msg.toUpperCase

String Methods with Arguments


let tvShow = 'catdog';

tv.Show.indexOf('cat'); 
tv.Show.indexOf('dog'); 
tv.Show.indexOf('z'); 

Enter fullscreen mode Exit fullscreen mode

indexOf is going to give us the string index and positional number where a given argument occurs in a string.

thing.method(arg)
Some methods accept arguments that modify their behavior.
Think of them as inputs that we can pass in.
We pass these arguments inside of the parentheses.

String Template Literals


`I counted ${3 + 4} sheep`; 

Enter fullscreen mode Exit fullscreen mode

Template literals are strings that allow embedded expressions, which will be evaluated and then turned into a resulting string.

Back ticks ` are used for template literals not single quotes '

Undefined and Null

Null is the intentional absence of any value, must be assigned.
Undefined variables that do not have an assigned value are undefined.

Top comments (0)