Skip to content

Instantly share code, notes, and snippets.

@joannasese
Last active April 30, 2017 18:49
Show Gist options
  • Save joannasese/79c21ec31c78e75866100dd63f86645b to your computer and use it in GitHub Desktop.
Save joannasese/79c21ec31c78e75866100dd63f86645b to your computer and use it in GitHub Desktop.
String Manipulation
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="String manipulation">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>String Manipulation</title>
</head>
<body>
<script id="jsbin-javascript">
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="String manipulation">
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>String Manipulation</title>
</head>
<body>
</body>
</html>
</script>
</body>
</html>
/*
STRING MANIPULATION
By now we know what a string is, and we know we can concatenate using the + operator.
There's more information we can pull from strings. For example, we can pull specific letters from strings.
To do this, we would treat the characters in the string like
values in an array, and assign them an index value starting with 0.
*/
var myString = "gardens"; // g = 0, a = 1, r = 3, and so on.
console.log(myString[5]); // prints -> n. Note the usage of bracket notation.
console.log(myString[myString.length - 1]); // prints -> s. use length - 1 to get the last value in an array (or last character in a string).
//I bet you're thinking.. why doesn't
console.log(myString.length - 1)
// print s ? Because here you're calling for the length of myString (7 for the seven letters in gardens) and subtracting 1.
console.log(myString.length - 1); // prints 6
//And how about
console.log(myString[length - 1])
// It prints undefined, because within the brackets, you haven't identified what you're trying to get the length of, which equates to there basically being nothing between the brackets.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment