1.

Why JavaScript 'var null' throw error but 'var undefined' doesn't?

Answer»

The Array.from() method is used in JavaScript to convert the arguments object to an array.

Let us FIRST see what Array.from() method PROVIDES us:

Array.from(obj, mapFunction, thisValue)

Here,

  • obj: The object which is converted to array
  • mapFunction: Map Function to be called
  • val: Value to be used as this while executing the map function.

Here, we have a function, which we are calling with some values:

display('q', 'w', 'h', 'm', '20', '66', '35')

The display() function CONSIDER arguments and sets using the Array.from() method. With that the sort() method is used to sort the array for our example: 

function display(){    var myArgs = Array.from(arguments);    return myArgs.sort(); }

The following is an example to convert the arguments object to an array: 

<!DOCTYPE html> <html> <BODY> <script>   function display(){     var myArgs = Array.from(arguments);     return myArgs.sort();   }  document.write(display('q', 'w', 'h', 'm', '20', '66', '35')); </script> </body> </html>

The output:

20,35,66,h,m,q,w


Discussion

No Comment Found