Assignment GDB Solution: Javascript Basic


JavaScript Throw Statement

JavaScript Throw Statement The throw statement generates an error condition that can be handled by a try…catch statement. The throw statement allows you to create an exception. The throw statement allows you to create an exception. If you use this statement together with the try…catch statement, you can control program flow and generate accurate error […]

JavaScript For…In Statement

JavaScript For…In Statement The for…in statement is used to loop (iterate) through the elements of an array or through the properties of an object. The for…in statement iterates a specified variable over all the properties of an object. For each distinct property, JavaScript executes the specified statements. A for…in statement looks as follows: Syntax for (variable in […]

JavaScript Break and Continue Statements

There are two special statements in JavaScript that can be used inside loops: break and continue. Break Statement We Use the break statement to terminate a loop, switch, or label statement. When we use break without a label, it terminates the innermost enclosing while, do-while, for, or switch immediately and transfers control to the following […]

Javascript unshift array method

unshift Adds one or more elements to the beginning of an array and returns the new length of the array. Syntax arrayName.unshift(element1, …, elementN) Example // assumes a println function exists myFish = [“angel”, “clown”]; println(“myFish before: ” + myFish); unshifted = myFish.unshift(“drum”, “lion”); println(“myFish after: ” + myFish); println(“New length: ” + unshifted); Output […]

Javascript Shift array method

Shift Removes the first element from an array and returns that element. This method changes the length of the array. Syntax array.shift() Example // assumes a println function is defined var myFish = [“angel”, “clown”, “mandarin”, “surgeon”]; println(“myFish before: ” + myFish); var shifted = myFish.shift(); println(“myFish after: ” + myFish); println(“Removed this element: ” […]

Javascript Splice array method

Splice Changes the content of an array, adding new elements while removing old elements. Syntax array.splice(index, howMany[, element1[, …[, elementN]]]) Example // assumes a print function is defined var myFish = [“angel”, “clown”, “mandarin”, “surgeon”]; print(“myFish: ” + myFish); var removed = myFish.splice(2, 0, “drum”); print(“After adding 1: ” + myFish); print(“removed is: ” + […]