Difference Between Shift and Pop Methods in JavaScript

Ayush Gupta
Updated on 16-Sep-2019 07:03:11

516 Views

The shift method removes the element at the zeroeth index and shifts the values at consecutive indexes down, then returns the removed value. If the length property is 0, undefined is returned.The pop() method removes the last element from an array and returns that element. This method changes the length of the array.Examplelet fruits = ['apple', 'mango', 'orange', 'kiwi']; let fruits2 = ['apple', 'mango', 'orange', 'kiwi']; console.log(fruits.pop()) console.log(fruits2.shift()) console.log(fruits) console.log(fruits2)Outputkiwi apple [ 'apple', 'mango', 'orange' ]  [ 'mango', 'orange', 'kiwi' ] Note that both the original arrays were changed here.Shift is slower than pop because it also needs to shift all ... Read More

Low-Level Difference Between Slice and Splice Methods in JavaScript

Ayush Gupta
Updated on 16-Sep-2019 06:56:26

425 Views

The basic difference between slice and splice is −splice() changes the original array on which it is called and returns the removed item(s) in an array as a new array object.slice() doesn't change the original array and also returns the array sliced.Example// splice changes the array let arr = [1, 2, 3, 4, 5]; console.log(array.splice(2)); //slice doesn't change original one let arr2 = [1, 2, 3, 4, 5]; console.log(array2.slice(2)); console.log(" After Changing the arrays"); console.log(array); console.log(array2);Output[ 3, 4, 5 ] [ 3, 4, 5 ]After Changing the arrays[[ 1, 2 ] [ 1, 2, 3, 4, 5 ]

Difference Between JavaScript Deep Copy and Shallow Copy

Ayush Gupta
Updated on 16-Sep-2019 06:52:07

254 Views

Shallow copy and deep copy are language agnostic. Shallow copies duplicate as little as possible. A shallow copy of a collection is a copy of the collection structure, not the elements. With a shallow copy, two collections now share the individual elements.Examplelet innerObj = {    a: 'b',    c: 'd' } let obj = {    x: "test",    y: innerObj } // Create a shallow copy. let copyObj = Object.assign({}, obj); // Both copyObj and obj's prop y now refers to the same innerObj. Any changes to this will be reflected. innerObj.a = "test" console.log(obj) console.log(copyObj)Output{ x: 'test', ... Read More

Radix Sort in JavaScript

Ayush Gupta
Updated on 16-Sep-2019 06:50:09

266 Views

The radix sort algorithm distributes integers into buckets based on a number's significant digit or value (the radix). The radix is based on the number system of the values of the arrays. Let us look at how it can be implemented −Examplefunction radixSort(arr) {    // Find the max number and multiply it by 10 to get a number    // with no. of digits of max + 1    const maxNum = Math.max(...arr) * 10;    let divisor = 10;    while (divisor < maxNum) {       // Create bucket arrays for each of 0-9     ... Read More

JavaScript Link: Hash vs javascript:void(0)

Smita Kapse
Updated on 13-Sep-2019 13:14:45

203 Views

Using “javascript:void(0)” is definitely better, since its faster. Try to run both the examples in Google Chrome with the developer tools. The “javascript:void(0)” method takes less time than the only #.Here’s the usage of “javascript: void(0)”:If inserting an expression into a web page results in an unwanted effect, then use JavaScript void to remove it. Adding “JavaScript:void(0)”, returns the undefined primitive value.The void operator is used to evaluate the given expression. After that, it returns undefined. It obtains the undefined primitive value, using void(0).The void(0) can be used with hyperlinks to obtain the undefined primitive valueExampleLive Demo     ... Read More

What is a Fat Arrow Function in JavaScript

Samual Sam
Updated on 13-Sep-2019 08:40:44

640 Views

Fat arrow function as the name suggests helps in decreasing line of code. The syntax => shows fat arrow. This also avoids you to write the keyword “function” repeatedly.Here’s the syntax:argument => expressionUse the following for more than one argument:(argument1 [, argument2]) => expressionLet’s compare a function with and without fat arrow:Function in JavaScriptvar rank = [7,8,9]; var display = rank.map(function(num) {    return num * num; });Fat Arrow function in JavaScriptvar rank= [7,8,9]; var display = rank.map((num) => num*num); document.write(arr)Arrow function definitely reduces the code lines.

Unset a JavaScript Variable

Sai Subramanyam
Updated on 13-Sep-2019 07:44:06

17K+ Views

To unset a variable in JavaScript, use the undefined. After that, use delete operator to completely remove it.Let’s try the code snippet again.var str = “Demo Text”; window.onscroll = function() {    var y = this.pageYOffset;    if(y > 200) {       document.write(nxt);       nxt = undefined; // unset      delete(nxt); // this removes the variable completely      document.write(nxt); // the result will be undefined } }

Best Way to Declare Multiple Variables in JavaScript

Amit Sharma
Updated on 13-Sep-2019 07:37:26

220 Views

Definitely, the following way of declaring multiple variables is more efficient:var variable1 = 5; var variable2 = 3.6; var variable3 = “Amit”;Suppose you need to add, remove, or update a variable, then you can easily do it.But with the following method, you need to do more changes. For example, on removing a variable, you need to remove the semi-colon. If it’s the first, then you need to add var to the second variable.var variable1 = 5, variable2 = 3.6, variable3 = “Amit”

Data Stored in var and How Its Content Changes in JavaScript

Amit Sharma
Updated on 13-Sep-2019 07:26:07

100 Views

JavaScript variables are not typed but their values do have a type. The same variable can be assigned new values.ExampleLive Demo                    var a;          document.write(typeof a+"\r");          a = true;          document.write(typeof a+"\r");          a = 5;          document.write(typeof a+"\r");          a = "web";          document.write(typeof a+"\r");          

Throw Unchecked Exception from a Static Block in Java

Maruthi Krishna
Updated on 12-Sep-2019 08:57:15

2K+ Views

A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.Example Live Demopublic class MyClass {    static{       System.out.println("Hello this is a static block");    }    public static void main(String args[]){       System.out.println("This is main method");    } }OutputHello this is a static block This is main methodExceptions in static blockJust like any other method in Java when an exception occurs in static block you can handle it using try-catch ... Read More

Advertisements