Embed presentation
Downloaded 132 times





![www.webstackacademy.com ‹#›Strings - ImmutableExample:<script>var str = “Hello World”;str[0] = “h”;console.log(str); //will be “Hello World”</script>• In JavaScript strings are immutable or unchangeable.• An immutable object is an object whose state cannot be modified after it is created.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-6-2048.jpg&f=jpg&w=240)

























![www.webstackacademy.com ‹#›Array(Syntax – Using [ ] )Syntax :// Creates initialized arrayvar array-name = [item1, item2, …];// Creates emptyvar array-name = [ ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-32-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›Array - ExampleExample :var array = [10, 20, 30, 40, 50];Orvar array = new Array(10, 20, 30, 40, 50);Orvar array = Array(3);](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-34-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›Array Access – Using loopfor ( let idx = 0; idx < 5; idx++ ) {document.write(“Element at index ” + idx + “ is ” + myArray[idx]);}for ( let idx in myArray ) {document.write(“Element at index ” + idx + “ is ” + myArray[idx]);}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-35-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›Array - HeterogeneousEach element in an array can be of different typesvar mixArray = [“Student”, 95, true];for(let idx = 0; idx < 3; idx++) {console.log (mixArray[idx]);}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-36-2048.jpg&f=jpg&w=240)





![www.webstackacademy.com ‹#›The join() Method The join() method converts all the elements of an array to strings andconcatenates them, returning the resulting string It behaves like toString(), but we can specify separatorExample :var fruits = ["Banana", "Orange","Apple", "Mango"];var str = fruits.join(" + ");document.write(str);](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-42-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The push() and pop() methodsStack operations – LIFOvar numStack = [10, 20, 30, 40, 50];numStack.pop(); // Pop an elementfor (let idx=0; idx <numStack.length; idx++) {document.write(numStack[idx] + "<br>");}numStack.push(100); // Push an elementfor (let idx=0; idx <numStack.length; idx++) {document.write(numStack[idx] + "<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-43-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The shift() methodsExample:var array1 = [1, 2, 3];var firstElement = array1.shift();console.log(array1);// expected output: Array [2, 3]console.log(firstElement);// expected output: 1The shift() method removes the first element from an array and returnsthat removed element.Syntaxarray.shift();](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-44-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The unshift() methodsExample:var array1 = [1, 2, 3];array1.unshift(2);console.log(array1);array1.unshift(5,8);console.log(array1);The unshift() method adds one or more elements to the beginning of anarray and returns the new length of the array.Syntaxarray.unshift();](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-45-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The sort() MethodThe sort() method sorts an array in alphabetic ordervar numList = [‘banana’, ‘apple’, ‘mango’, ‘grapes’];numList.sort();for (let idx = 0; idx < numList.length; idx++){document.write(numList[idx] + "<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-46-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The reverse() MethodThe reverse() method reverses the elements in an arrayvar numList = [55, 3, 16, 21];numList.reverse();for(let idx = 0; idx < numList.length; idx++){document.write(numList[idx] + "<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-47-2048.jpg&f=jpg&w=240)


![www.webstackacademy.com ‹#›Thesplice() Methodvar ar = [1, 4, 9, 10, 11, 16];ar.splice(1,3);// will remove the elementsconsole.log(ar);ar.splice(1,0,5);// will insert the element 5 at index 1console.log(ar);www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-50-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›TheforEach() MethodExample:var age = [21,19,34,56,23,20,17,65,76,15,35,14,13,25];age.forEach(function(item) {document.write(item+" ");});www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-52-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›Thefilter() MethodExample:var words = ['limit', 'elite', 'exuberant', 'destruction‘];function word(elem) {return elem.length > 6;}var result = words.filter(word);console.log(result);// expected output: Array ["exuberant", "destruction“];www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-54-2048.jpg&f=jpg&w=240)


![www.webstackacademy.com ‹#›Themap() MethodExample:var curArray = [1, 4, 9, 16];// pass a function to mapvar doubleArray = curArray.map(doubleItem);function doubleItem(currentItem) {return 2 * currentItem;}console.log(doubleArray);www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-57-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›Exercisewww.webstackacademy.com ‹#›Write a JavaScript program to greet the guest.For example:1. Input : [‘Ram’,’Radhika’,’John’,’Sumit’]Output: Welcome RamWelcome RadhikaWelcome JohnWelcome Sumit](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-58-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›Multidimensional ArrayDeclaring Multidimensional Arrayvar array2d = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ];for(let idx = 0; idx < array2d.length; idx++) {for(let jdx = 0; jdx < array2d[idx].length; jdx++) {document.write(array2d[idx][jdx] + " ");}document.write("<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-60-2048.jpg&f=jpg&w=240)



The document provides an extensive overview of strings and arrays in JavaScript, detailing their characteristics, methods, and usage. It covers string properties such as immutability and common methods like length, concat, trim, and various other string manipulation techniques. Additionally, it explains arrays as collections of elements with methods for accessing, modifying, and performing operations on them, alongside practical exercises for applying these concepts.





![www.webstackacademy.com ‹#›Strings - ImmutableExample:<script>var str = “Hello World”;str[0] = “h”;console.log(str); //will be “Hello World”</script>• In JavaScript strings are immutable or unchangeable.• An immutable object is an object whose state cannot be modified after it is created.](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-6-2048.jpg&f=jpg&w=240)

























![www.webstackacademy.com ‹#›Array(Syntax – Using [ ] )Syntax :// Creates initialized arrayvar array-name = [item1, item2, …];// Creates emptyvar array-name = [ ];](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-32-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›Array - ExampleExample :var array = [10, 20, 30, 40, 50];Orvar array = new Array(10, 20, 30, 40, 50);Orvar array = Array(3);](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-34-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›Array Access – Using loopfor ( let idx = 0; idx < 5; idx++ ) {document.write(“Element at index ” + idx + “ is ” + myArray[idx]);}for ( let idx in myArray ) {document.write(“Element at index ” + idx + “ is ” + myArray[idx]);}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-35-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›Array - HeterogeneousEach element in an array can be of different typesvar mixArray = [“Student”, 95, true];for(let idx = 0; idx < 3; idx++) {console.log (mixArray[idx]);}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-36-2048.jpg&f=jpg&w=240)





![www.webstackacademy.com ‹#›The join() Method The join() method converts all the elements of an array to strings andconcatenates them, returning the resulting string It behaves like toString(), but we can specify separatorExample :var fruits = ["Banana", "Orange","Apple", "Mango"];var str = fruits.join(" + ");document.write(str);](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-42-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The push() and pop() methodsStack operations – LIFOvar numStack = [10, 20, 30, 40, 50];numStack.pop(); // Pop an elementfor (let idx=0; idx <numStack.length; idx++) {document.write(numStack[idx] + "<br>");}numStack.push(100); // Push an elementfor (let idx=0; idx <numStack.length; idx++) {document.write(numStack[idx] + "<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-43-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The shift() methodsExample:var array1 = [1, 2, 3];var firstElement = array1.shift();console.log(array1);// expected output: Array [2, 3]console.log(firstElement);// expected output: 1The shift() method removes the first element from an array and returnsthat removed element.Syntaxarray.shift();](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-44-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The unshift() methodsExample:var array1 = [1, 2, 3];array1.unshift(2);console.log(array1);array1.unshift(5,8);console.log(array1);The unshift() method adds one or more elements to the beginning of anarray and returns the new length of the array.Syntaxarray.unshift();](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-45-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The sort() MethodThe sort() method sorts an array in alphabetic ordervar numList = [‘banana’, ‘apple’, ‘mango’, ‘grapes’];numList.sort();for (let idx = 0; idx < numList.length; idx++){document.write(numList[idx] + "<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-46-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›The reverse() MethodThe reverse() method reverses the elements in an arrayvar numList = [55, 3, 16, 21];numList.reverse();for(let idx = 0; idx < numList.length; idx++){document.write(numList[idx] + "<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-47-2048.jpg&f=jpg&w=240)


![www.webstackacademy.com ‹#›Thesplice() Methodvar ar = [1, 4, 9, 10, 11, 16];ar.splice(1,3);// will remove the elementsconsole.log(ar);ar.splice(1,0,5);// will insert the element 5 at index 1console.log(ar);www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-50-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›TheforEach() MethodExample:var age = [21,19,34,56,23,20,17,65,76,15,35,14,13,25];age.forEach(function(item) {document.write(item+" ");});www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-52-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›Thefilter() MethodExample:var words = ['limit', 'elite', 'exuberant', 'destruction‘];function word(elem) {return elem.length > 6;}var result = words.filter(word);console.log(result);// expected output: Array ["exuberant", "destruction“];www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-54-2048.jpg&f=jpg&w=240)


![www.webstackacademy.com ‹#›Themap() MethodExample:var curArray = [1, 4, 9, 16];// pass a function to mapvar doubleArray = curArray.map(doubleItem);function doubleItem(currentItem) {return 2 * currentItem;}console.log(doubleArray);www.webstackacademy.com ‹#›](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-57-2048.jpg&f=jpg&w=240)
![www.webstackacademy.com ‹#›Exercisewww.webstackacademy.com ‹#›Write a JavaScript program to greet the guest.For example:1. Input : [‘Ram’,’Radhika’,’John’,’Sumit’]Output: Welcome RamWelcome RadhikaWelcome JohnWelcome Sumit](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-58-2048.jpg&f=jpg&w=240)

![www.webstackacademy.com ‹#›Multidimensional ArrayDeclaring Multidimensional Arrayvar array2d = [ [10, 20, 30], [40, 50, 60], [70, 80, 90] ];for(let idx = 0; idx < array2d.length; idx++) {for(let jdx = 0; jdx < array2d[idx].length; jdx++) {document.write(array2d[idx][jdx] + " ");}document.write("<br>");}](/image.pl?url=https%3a%2f%2fimage.slidesharecdn.com%2f010stringsandarrays-190503093705%2f75%2fJavaScript-Chapter-10-Strings-and-Arrays-60-2048.jpg&f=jpg&w=240)

