1 of 10

Please make sure your camera is on and your name is your First and Last Name

We will start at 5:05 pm

2 of 10

JAVASCRIPT

03: STRINGS

Deep-dive into strings: indexing, concatenation, and methods!

3 of 10

RECALL

02: Variables

4 of 10

WHAT IS INDEXING

  • An index is the position a character is at
  • An index always starts at 0, not 1
  • Indexing is getting the value at the index’s position
  • Use [ ] to get the index
  • Getting an index longer than the length of a string will return an error

var language = "JavaScript";

console.log(language[0]); // "J"

console.log(language[4]); // "S"

console.log(language[100]); // Error

5 of 10

LENGTH OF A STRING

  • Gives you how many characters long a string is
  • Unlike an index, counting start from 1
  • Use .length

var language = "JavaScript";

console.log(language.length); // 10

6 of 10

ACCESSING THE LAST CHARACTER

  • We can get the last character of a string by doing .length-1
    • Since indexing starts at 0 but length starts at 1
    • If we log .length without -1, it gives us an error

var language = "JavaScript";

console.log(language[language.length-1]); // "t"

console.log(language[language.length]); // Error

7 of 10

STRING CONCATENATION

  • Combining two strings together
  • Use +
  • Remember to use + " " + when necessary to add spaces between

var firstName = "Coding";

var lastName = "Power";

var fullName = firstName + " " + lastName;

console.log("Hello, " + fullName + "!"); // "Hello, Coding Power!"

8 of 10

STRING METHODS

var name = "Coding Power";

console.log(name.toUpperCase()); // "CODING POWER"

console.log(name.toLowerCase()); // "coding power"

console.log(name.slice(0, 6)); // "Coding"

  • .length to get the length (as we already saw)
  • .toUpperCase makes everything capitalized
  • .toLowerCase makes everything lowercase
  • .slice takes a piece of the string based on the start and end index
    • The start index is included but the end index is not!

9 of 10

TRY IT YOURSELF

Go to onecompiler.com/javascript

Pick a word and take a splice of it that has another word in it! (ex:

“The tulips are nice”)

nice tulips

10 of 10

THANKS!

Any questions?

codingpower101@gmail.com

codingpower.org