Doubts and Clarifications

Javascript

1. How to set an image src in JS?

In PIG Game, we set image src like this,

document.querySelector('.img').src = "img.png"

The doubt is, when do we use load event since setting img src is async operation?

If you want to do something after the image loads, the you capture load event and write the required code in a callback function.

If you don't want to know when the image has been loaded and not interested in performing any operations after the image has been loaded then you can proceed without capturing the load event. Example below

img.src = "test.jpg"
img.addEL('load',function(){
   console.log("The image has been fully loaded now");
})

2. Spread operator [...] creates a shallow copy of array. Can we create a deepcopy and how? Also for objects?

You can create the deep copy yourself manually but it's too much of work. Look at JavaScript notes and I've explained various ways to do that.

3. In react, a variable is not working that is defined outside JSX

React slider project john's milga https://www.youtube.com/watch?v=a_7Z7C_JCyo&t=11303s&ab_channel=freeCodeCamp.orgfreeCodeCamp.org

Last updated