strings

In this module we'll discuss strings.

Description

Strings are used in every application that you'll ever use, usernames, passwords, lists of products, and anything else that will have a plain English value(other languages too). Strings are a group of characters "strung" (concatenated/added) together. Strings must go in quotes.

File Location

You should be located in the following file:

   javascript-library
        └── 0-PreWork
            └── 1-HTML-Basics
            └── 2-CSS-Basics
            └── 3-JavaScript-Basics
                03-strings.js       <---- this file in this module.

String Examples

Let's print a few strings to the console:

console.log("I was born in Indianapolis.");
console.log("I've lived in three states.");
console.log("I like Indiana the best.");
console.log("My bank account had two million bucks in it. It's gone now.");

Variables

Just like integers, we need the var keyword(or const or let) to hold the value in a container. Here are a few examples:

var tweet = "Lebron is king! Westbrook for president!";
var facebookPost = "Just thought I'd share this goofy video of my dog eating our couch.";
var username = 'jamespauloconnor';

Numbers in a String

You can use numbers in a string, but you won't be able to do math operations. Numbers in quotes are treated as text.

var password = "123456789HELLO";
var birthDay = "August 11";

Concatenation

Concatenating happens when we add 2 strings together.

var birthCity = "Indianapolis ";
var birthState = "Indiana";

console.log(birthCity + ", " + birthState);

Challenge

QUICK CHALLENGE: See if you can print your own birth city and birth state to the console. What would you have to do?

Strings and Numbers

We can mix strings and integers together.

var ageInAugust = 40; 
var highSchool = "Bill Murray High School";
var graduatedHighSchool = 1994;

console.log(highSchool + commaWithSpace + graduatedHighSchool);
console.log("My age in August:", ageInAugust);

Another Challenge:

Write a string that has two variables. Output I graduated from Bill Murray High School in 1994.

var gradYear = 1994;
var highSchool = "Bill Murray High School";

console.log("I graduated from " + highSchool + " in " + gradYear + ".");

String Methods

String Methods allow us to introduce functions w/o having to worry about writing them. In JS, Strings, Numbers, and Booleans (not yet covered) have some built in properties and methods that are available through using the . operator.

We'll talk more about this in the future, but here are a few items to get you familiar with the syntax:

//length - returns the length of a string (including spaces)
console.log(highSchool.length); 

//lower or upper case
console.log(highSchool.toUpperCase()); //BILL MURRAY HIGH SCHOOL
console.log(highSchool.toLowerCase()); //bill murray high school

console.log(highSchool.split(" ")); //splits the string into an array on each space['Bill', 'Murray', 'High', 'School' ]
console.log(highSchool.slice(10)); // cuts off everything before the nth index

Last updated