1/14 Create New Variable


Aren't we supposed to add 'var' before $h1 while making the variable?

It gives an error message for var $h1=$("<h1>Hello</h1>); but passes this: $h1=$("<h1>Hello</h1>);

Why so?




google search
=the book=
javascript variable site:developer.mozilla.org

== discussions / opinions ==
javascript variable explained site:stackoverflow.com


Neither of that works for me


Try it with

var $h1=$("<h1>Hello</h1>");

add an extra double-quote-" to close the string "<h1>Hello</h1>"


Old topic, but I just ran into this issue myself. They're apparently looking for a global variable here.

This works:

var $h1 = $("<h1>Hello</h1>");


This does not work:

     var $h1 = $("<h1>Hello</h1>");

This also works:

     $h1 = $("<h1>Hello</h1>");

Removing "var" from within the function defaults to the nearest scope. In this case the scope is undefined, therefore it defaults to global.

Thank you for the reference links @leonhard.wettengmx.n


Now I don't understand where to put var and where not to


why did this code work?


Hey , could you help me plz.

    var $h1 = $("<h1>Hello</h1>");


    $h1 = $("<h1>Hello</h1>");

it gives me an answer "Oops, try again. Did you create a variable called $h1?"
I don't know what is wrong...



Please re-edit your Post

  • leave one blank-line above of your code
  • select your code in the Post
  • then =click= on the </>-symbol-of-this-editor

Your code will then be in a pre-code state
and you will be able to make/present the proper indentations.

With Courtesy to stetim94

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351



Try it by using single-quotes-' '
instead of using your double-quotes-" "