-1

So I'm supposed to write a function called laugh() that takes one parameter, num that represents the number of "ha"s to return. In the course I'm taking to learn JavaScript it doesn't explain clearly the purpose of the var ha = ''; Why is this string empty and what's the purpose of it being empty? Thanks for the help in advance.

function laugh(num) {
    var ha = '';

    for (i = 0; i < num; i++) {
      ha = ha + 'ha';
    }
    return ha + '!';
}

console.log(laugh(3));   
Jonathan Wood
  • 65,341
  • 71
  • 269
  • 466
timSully
  • 137
  • 1
  • 11
  • initialiases the variable as a string so `+` works as it should – Jaromanda X Jun 05 '18 at 03:35
  • if you don't do that, then the line `ha = ha + 'ha';` will throw error either because `ha` won't be a string or is undefined or null – Priyesh Kumar Jun 05 '18 at 03:35
  • 1
    @timSully I recommend you to go to console and try declaring a variable `var ha`, print its value, then performing `ha = ha + 'ha'`, and then again print its value. This should help you clear your confusion. – Chris Aby Antony Jun 05 '18 at 04:08

2 Answers2

2

Since javascript is dynamically typed(variable datatypes can be changed dynamically) the declaratoin var ha; doesn't necessarily make it a string. Also when a variable is declared in javascript and is not defined(ie; given value) its value is set as undefined by default.

If you haven't declared var ha; then ha = ha + 'ha'; will produce ReferenceError: xa is not defined error.

Again understand that undefined and 'not defined' are not the same as pointed out in comments. Refer this undefined vs not defined.

If you have declared var ha; , when you try to perform ha = ha + 'ha'; for the first time it translates to undefined + 'ha'. Here you are attempting to concatenate a string ('ha') to undefined. So in the first case it will make ha = 'undefinedha'. So declaring and assigning ha to an empty string as var ha = ''; solves this issue.

You can think of this as similar to how we commonly set a count variable count=0 in usual programs.

  • you're deeply wrong. It will never return 'undefinedha', instead it will throw a ReferenceError: ha is not defined, unless ha is defined in some outer scope. – Victor Lia Fook Jun 05 '18 at 04:40
  • @VictorLiaFook that is if you don't declare it. If you read the answer carefully you would have noticed I did mention declaring ha as `var ha;`. Thanks for pointing out though. It might lead to confusion, I will add that case as well to answer. – Chris Aby Antony Jun 05 '18 at 04:47
  • 1
    ok, gotcha. It is worth pointing out that the undefined is a primitive type in js and what happens when you don't assign a value is: the value undefined, which is the single value for the undefined primitive type is assigned automatically. So, let's not think that somehow the undefined value is a string by itself : the concat operator is responsible for this "cast" in your example. – Victor Lia Fook Jun 05 '18 at 04:58
-1

You need to return a string which should have X number of 'ha' where X is provided as your function argument . You need to initialize a variable of string type before you can add any content to it. This is just like you have an office desk where you need to separate all the stationary from electronics. You would gather all the stationary and electronics but before you put them again on the desk you need a separate drawer for them . The drawer will be initially empty. Now read the following code again

var drawerForStationary=''; //Empty Drawer
var stationary=["pen","pencil","stapler"]; //stationary gathered 
for(i=0;i<stationary.length;i++)
{
    drawerForStationary+=stationary[i]; //Putting stationary one by one to drawer
}

Now if you don't initialize the string as an EMPTY string there are chances of getting UNEXPECTED RESULTS as you have not told the compiler anything about the new variable so it has to assume what kind of data this variable will hold.