JavaScript JavaScript and the DOM Making Changes to the DOM DOM Manipulation

Robert O'Toole
Robert O'Toole
6,318 Points

does anyone find the need for (' ') vs ( ) somewhat ridiculous ?

the way .appendchild(string) doesnt require quotations in the string but then things like createElement('string') do require quotations... then getElementsbyTagName requires capitalization and quotations ('STRING') but then query selectors require ('string'). is this just a shortcoming of the language? is this me not understanding something that should be more consistent in my thought process?

its honestly driving me nuts and its usually the only mistake i make on this stuff. does anyone have a clear and concise way of learning this? or is this just something i have to constantly struggle with and google

hopefully you guys understand what im saying here. partly venting partly asking for help

2 Answers

Steven Parker
Steven Parker
182,438 Points

It sounds like you may be confusing string literals with string variables. String literals are always enclosed in quotes, and they represent the characters that are between the quotes (and can never change).

On the other hand, variable names are never enclosed in quotes, and what they represent depends on what they were most recently assigned with (which can be changed at any time).

A function that takes a string argument (like appendchild) can be passed either a variable or literal. Here's two examples:

someElement.appendChild(MyStringVariable);         // passing a string variable argument
someElement.appendChild("<button>Stop</button>");  // passing a string literal argument

Finally, tag names are stored internally in upper case for legacy reasons, but they are an exception.

Robert O'Toole
Robert O'Toole
6,318 Points

thank you so much I think I overlooked this... I was wondering why this all seemed so inconsistent for me and I knew there was a proper method to passing strings! thanks again!