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 arraylet myString = ""; // Declaration of an empty stringconst starWars = ["Rey", "Finn", "Han Solo"]; // Declaration and Initializationlet prolog = "A long time ago in a galaxy far, far away..."console.log(starWars[0]); // Getting the value stored at an array index locationconsole.log(prolog[0]); // Getting a character from a string at an index locationconsole.log(prolog.charAt(0)); // Another way to do the same thing as aboveconsole.log(starWars.length); // Getting the array lengthconsole.log(prolog.length); // Getting the string lengthxxxxxxxxxxstarWars[starWars.length] = "Yoda"; // Adding a new element and valueprolog += "A New Hope"; // Adding another string (more characters)
xxxxxxxxxxconsole.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.