1/14 Create New Variable


#1

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?


#2

@netrockstar24563,

Reference::

google search
=the book=
javascript variable site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Values,_variables,_and_literals

== discussions / opinions ==
javascript variable explained site:stackoverflow.com
http://stackoverflow.com/questions/1470488/what-is-the-function-of-the-var-keyword-and-when-to-use-it-or-omit-it


#3

Neither of that works for me


#4

@thorlindur99,
Try it with

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

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


#5

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>");

$(document).ready(function(){});

This does not work:


$(document).ready(function(){
     var $h1 = $("<h1>Hello</h1>");
});

This also works:


$(document).ready(function(){
     $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


#6

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


#7

why did this code work?


#8

Hey , could you help me plz.

$(document).ready(function(){
    var $h1 = $("<h1>Hello</h1>");
});

OR

$(document).ready(function(){
    $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...


#10

@pyfocus

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
https://discuss.codecademy.com/t/im-having-a-problem-on-lesson-11-23/2307/3?u=leonhard.wettengmx.n

or even better use
= https://discuss.codecademy.com/t/how-do-i-format-code-in-my-posts/28351
[extra's]
https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet


#11

@pyfocus

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