Question

In JavaScript What is the result? Make sure you can explain why. let arr = ["a",...

In JavaScript

What is the result? Make sure you can explain why. let arr = ["a", "b"]; arr.push(function() { alert( this ); }) arr[2](); // ?

0 0
Add a comment Improve this question Transcribed image text
Answer #1

the output is shown in the image above. the thing we are doing here, is known as Closure, a very prominent feature of javascript.

We first created an array of 2 elements. Then we appended a function on the 3rd position of the array and then when we say arr[2](), then we are basically calling this function.

Inside the function body, we use alert(this); this always refers to the object in enclosing scope, which in this case is the array arr, because it keeps the function inside it.. Hence when we say alert(this), the entire content of array are string represented on the alert popup that you see above.

hope it clarifies.

Add a comment
Know the answer?
Add Answer to:
In JavaScript What is the result? Make sure you can explain why. let arr = ["a",...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • convert Javascript code to Python and make sure you use chained map and filter as well....

    convert Javascript code to Python and make sure you use chained map and filter as well. https://book.pythontips.com/en/latest/map_filter.html CODE BELOW IS IN JAVASCRIPT let people = [ {name: "Amy", pounds_weight: 152, inches_height: 63}, {name: "Joe", pounds_weight: 120, inches_height: 64}, {name: "Tom", pounds_weight: 210, inches_height: 78}, {name: "Jim", pounds_weight: 180, inches_height: 68}, {name: "Jen", pounds_weight: 120, inches_height: 62}, {name: "Ann", pounds_weight: 252, inches_height: 63}, {name: "Ben", pounds_weight: 240, inches_height: 72}, ]; //functions to convert pounds to kg and inches to meters let...

  • In this Javascript code, why are 2 for loops needed? could you please explain this to...

    In this Javascript code, why are 2 for loops needed? could you please explain this to me. This is the outcome * ** *** **** ***** ****** ******* ******** ********* the code is <html> <head>               <title></title>        <script>                  showStars(10);               function showStars(rows){            for(var i = 0; i < rows; i++){                var pattern = "";                for(var j...

  • What are the essential elements of a contract? Make sure you fully explain what each of...

    What are the essential elements of a contract? Make sure you fully explain what each of these elements are and why they are all needed to make a contract valid and enforceable. Your initial post of at least 250 words

  • Re-Write this code In Javascript without using the .slice method. Make sure there is still a...

    Re-Write this code In Javascript without using the .slice method. Make sure there is still a loop to show the first 5 objects in the array. function getData() { axios.get('https://jsonplaceholder.typicode.com/photos').then(resp => { //resp.data.slice(1, 5).forEach(element => {    resp.data.slice(1,2).forEach(element => {    $("#tasks").html($("#tasks").html() + ` <div> <img src="${element.url}" height="300" alt="${element.title}" /> <p>${element.title}</p>   </div> `) }); }); }// JavaScript Document

  • Why the occurrence of mutations is essential to the evolutionary process? Make sure you briefly explain...

    Why the occurrence of mutations is essential to the evolutionary process? Make sure you briefly explain the relation to protein synthesis and the harmful, neutral or beneficial consequences of mutations.

  • Make sure you can explain each of these Suppose a new innovation increases the marginal product...

    Make sure you can explain each of these Suppose a new innovation increases the marginal product of workers in a competitive industry. This will shift the marginal cost of production of individual firms to the (right/ left). The zero-profit price will be (higher/ lower). In the long run, the market price will be (higher/ lower/ the same), individual firms will earn (negative/ positive/ zero) economic profits, and market quantity of the good will be (higher/ lower/ the same). Make sure...

  • MAKE SURE TO INCLUDE WHAT PORTION OF THE CODE GOES IN WHAT FILE FOR THE CLASS....

    MAKE SURE TO INCLUDE WHAT PORTION OF THE CODE GOES IN WHAT FILE FOR THE CLASS. AND A PARAGRAPH DESCRIPTION OF THE CODE AND HOW IT WORKS!!! Please include all the steps and ill give you a great rating! An array can be used to store large integersone digit at a time. For example, the integer 1234 could be stored in the array a by settinga0 t, a1] to 2, a[2] to 3, and a[3] to 4. How-ever, for this...

  • JavaScript Write a function using recursion to compute the Fibonacci number of n (where n is...

    JavaScript Write a function using recursion to compute the Fibonacci number of n (where n is a positive integer). Your function should output the calculated result for the n given. You also need to type check to make sure the value being given is an integer.

  • i. Explain why this definite integral is an improper integral. ii. Determine if this improper integral converges or diverges. Be sure to treat the improper integral with appropriate mathematical rigou...

    i. Explain why this definite integral is an improper integral. ii. Determine if this improper integral converges or diverges. Be sure to treat the improper integral with appropriate mathematical rigour. Simply treating the improper integral as if it was a proper integral will result in zero marks. Furthermore, make sure you clearly explain/justify each step in your limit analysis working. thanks for your answer, please give a clear writing. (b) Consider the definite integral 2 1 i. Explain why this...

  • This question relates to Javascript. I know that a function can return 1 value or no...

    This question relates to Javascript. I know that a function can return 1 value or no values. What are some examples of Javascript functions returning no values? Why is this useful? If the function does not return any value then why and how is it useful?

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT