CS133JS Beginning Programming: JavaScript
Topics by Week | |
---|---|
1. Intro to JavaScript programming | 6. Arrays |
2. Functions, Operators and Expressions | 7.Objects and Object Constructors |
3. Conditional Statements: if and switch | 8. Web Page I/O |
4. Repetition: while, do while, and for | 9. Regular Expressions |
5. Midterm Check-Point | 10. Review |
Ways in Which Strings are Like ArraysDeclaration and InitializationArray and String Index OperationsGet a value or element at an index locationGet the lengthAdd a value or element to the endGet the index location of a value Reference
const myArray = []; // Declaration of an empty array
let myString = ""; // Declaration of an empty string
const starWars = ["Rey", "Finn", "Han Solo"]; // Declaration and Initialization
let prolog = "A long time ago in a galaxy far, far away..."
console.log(starWars[0]); // Getting the value stored at an array index location
console.log(prolog[0]); // Getting a character from a string at an index location
console.log(prolog.charAt(0)); // Another way to do the same thing as above
console.log(starWars.length); // Getting the array length
console.log(prolog.length); // Getting the string length
xxxxxxxxxx
starWars[starWars.length] = "Yoda"; // Adding a new element and value
prolog += "A New Hope"; // Adding another string (more characters)
xxxxxxxxxx
console.log(starWars.indexOf("Finn")); // Gets the index of the elmeent containing "Finn"
console.log(prolog.indexOf("g")); // Gets the index of the character "g"
JavaScript Strings on W3Schools
String on Mozilla Developer Network
Beginning JavaScript Lecture Notes by Brian Bird 2018, revised are licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.