How to store the content returned by the text()?

I’m trying to extract the entire text from the HTML document and I was able to see the desired o/p only on

$(document).ready(function(){
      console.log($("*").text())
}

and not on the following snippet.

var words = ""
$(document).ready(function(){ 
     words += ($("*").text())
}
console.log(words)

I want text which is getting displayed in the console, to be stored in a variable for computation.


It would be of immense help if anyone could tell me how to do it. Thanks in advance.

Hello Shernz,

In the second example, if that is the full code, you are not outputing words anywhere. In the first example, you are calling console.log, but in the second example you are not.

Sorry for not including that part. Actually I did. Thanks for pointing it shall edit the post.