JS-101-Fundamentals
  • JavaScript Library
  • 1-JavaScript Fundamentals
    • 0-Getting Started
      • Welcome
    • 1-JS-Fundamentals
      • 1-Grammar and Types
        • Comments
        • Declarations
        • Scope
        • Hoisting
        • Types
        • Literals
      • 2-Control Flow and Error Handling
        • if
        • if else
        • switch
        • try catch
        • throw
      • 3-Loops
        • For Loops
        • For In Loops
        • For Of Loops
        • Do While
        • While Loops
      • 4-Functions
        • Declarations
        • Expressions
        • Calling Functions
        • Scope
        • Parameters
        • Arguments
        • Closures
        • Closures Challenge
        • Arrow Functions
        • Predefined
      • 5-Expressions and Operators
        • Assignment
        • Comparison
        • Ternary
        • Typeof
        • Left Hand Side
        • Spread
      • 6 Numbers and Dates
        • Numbers
          • Numbers Enhanced
        • Math
        • Dates
      • 7 String Methods
        • String Literals
        • Methods
      • 8 Regular Expressions
        • Basic Intro
      • 9 Arrays
        • Array Review
        • Populating/Referring
        • Length
        • Iterating
        • Methods
      • 10 Objects
        • About Objects
        • Properties
        • Enumeration
        • Initializers
        • Constructor Functions
        • this
        • create
        • Methods
      • 11 ES6 Intro
        • ES6 Intro
        • let
        • const
Powered by GitBook
On this page
  • File Location
  • Practice
  • BONUS CHALLENGE
  • BONUS CHALLENGE 2
  1. 1-JavaScript Fundamentals
  2. 1-JS-Fundamentals
  3. 7 String Methods

Methods

PreviousString LiteralsNext8 Regular Expressions

Last updated 7 years ago

Like other object, the String object has many built-in methods and functions that you can access. Here are some of the most common:

Name

Description

length

This string property returns the length of the string.

charAt()

Returns the character at the given index of the string. If left blank, returns the character at index 0.

concat()

Takes an unlimited number of strings, concatenates them to the original string, and returns the result.

endsWith():

Returns true if the given character is at the last index of the string, otherwise returns false.

includes()

Returns true if the given character is present in the string, otherwise returns false.

indexOf()

Returns the first index location of the given character in the string if present. Returns -1 if not found.

lastIndexOf()

Returns the last index location of the given character in the string if present. Returns -1 if not found.

repeat()

Duplicates the original string the given number of times and returns the result.

slice()

Cuts the string in two at the given index and returns the result.

startsWith()

Returns true if the given character is at the first index of the string, otherwise returns false.

toUpperCase()

Capitalizes each letter in the original string and returns the result.

toLowerCase()

Converts each letter in the original string to lower case and returns the result.

trim()

Removes white space at either end of the original string and returns the result.

You can find more information on these and other String methods

File Location

We will be working in the following file:

    javascript-library
        └── 0-PreWork
        └── 1-Fundamentals
            └── 1-Grammar-and-Types
            └── 2-ControlFlow-and-ErrorHandling
            └── 3-Loops
            └── 4-Functions
            └── 5-Expressions-and-Operators
            └── 6-Numbers-and-Dates
            └── 7-StringMethods
                01-string-literals.js 
                02-methods.js <----You will be working in this file.

Practice

  1. Create a string and print it to the console, print the length of the string, then remove the last two characters of the string and print the result.

  2. Add the string "HELLO WORLD" to your current string at index 3 and print the result.

  3. Create one variable with the string EFA JS March 2018 and a second variable that is a blank string. Split the string at the M in March, put the result into the second variable, and print both to the console.

BONUS CHALLENGE

Create a string with your first name. Write a function to capitalize only the first letter and print the result to the console.

BONUS CHALLENGE 2

Create a string with your first and last name. Write a function to capitalize only the first letter of each and return the result.

here