Course Hero Logo

Preferences and when in doubt do the thing that i

Course Hero uses AI to attempt to automatically extract content from documents to surface to you and others so you can study better, e.g., in search results, to enrich docs, and more. This preview shows page 39 - 42 out of 141 pages.

preferences. And when in doubt: do the thing that I like best,because I am right.No, really, use whichever one you find more comfortable.Pretty sure I’m right, though.IdentifiersThe name we give a variable is called anidentifier.Like everything in JavaScript, identifiers are case-sensitive,and come with a few special rules, as well:JAVASCRIPT FOR WEB DESIGNERS34
They must start with a letter, underscore, or dollar sign—nota number.• They can’t contain spaces.• They can’t contain special characters (! . , / \ + - * =).There are a set of words in JavaScript that can’t be used asidentifiers, like null, for example. These are calledkeywordswords that already have an immutable meaning to JavaScript,or are set aside just in case they get added to JavaScript one day:abstract boolean break byte case catch char classconst continue debugger default delete do doubleelse enum export extends false final finallyfloat for function goto if implements import ininstanceof int interface long native new nullpackage private protected public return shortstatic super switch synchronized this throw throwstransient true try typeof var void volatile whilewithThat’s a scary block of words, but this isn’t something youneed to have committed to memory—I certainly don’t. It doesmake a good case for an editor with syntax highlighting, though,which can help you avoid mysterious-seeming errors whenassigning an identifier to a variable (FIG 2.7).FIG 2.7:Syntax highlighting can make iteasier to catch errors on the fly.35UNDERSTANDING DATA TYPES
Outside of these rules, an identifier can contain anycombination of letters, digits, and underscores. It’s agood idea to use identifiers that are brief (totalCostvs.valueOfAllItemsIncTaxAndShipping) and easy to understandat a glance (selectedValuevs.v1). The “foo,” “bar,” and “baz”that I’ve been using in my examples are lousy identifiers—thewords have no meaning whatsoever, so coming across themin a script would give you no clues as to the nature of the datathey contain. At the same time, we should avoid identifiersthat describe their potential values in too muchdetail, sincewe may not always be able to immediately predict the valuesa variable will contain. A variable originally namedmilesmayneed to contain a value in kilometers one day—confusing forthe developers who end up maintaining that code, ourselvesincluded.distanceworks much better.Variable scopeWe’ll get into this more when we look at functions, but wecan’t talk about variables without discussing something calledvariable scope.Think of variable scope as the section of your source codewhere you’ve assigned something an identifier. Outside of thatsection, that variable is not defined, and the identifier may bereused for something else. JavaScript applications can be huge,with tens of thousands of lines of code being parsed and exe-cuted. Because variables have their own scope, we can elect tomake them available to the entire applicationorconstrained to

Upload your study docs or become a

Course Hero member to access this document

Upload your study docs or become a

Course Hero member to access this document

End of preview. Want to read all 141 pages?

Upload your study docs or become a

Course Hero member to access this document

Term
Fall
Professor
NoProfessor
Tags
Document Object Model, T F O R W E B D E S I G N E R S

Newly uploaded documents

Show More

Newly uploaded documents

Show More

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture

  • Left Quote Icon

    Student Picture