Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member

Understanding the structure of JavaScript code

From: JavaScript Essential Training

Video: Understanding the structure of JavaScript code

So as you see, JavaScript is written as plain text, like HTML and our CSS, and JavaScript is what's called an interpreted language, not a compiled language. A lot of other languages need to be run through a special program called a compiler that takes your code, converts it into machine code, which can then run on the operating system, but we don't need to do that with JavaScript. We simply write our code and we hand it over to the web browser, and the web browser takes care all interpreting it and running it on the machine. Now, JavaScript is case sensitive.

Understanding the structure of JavaScript code

So as you see, JavaScript is written as plain text, like HTML and our CSS, and JavaScript is what's called an interpreted language, not a compiled language. A lot of other languages need to be run through a special program called a compiler that takes your code, converts it into machine code, which can then run on the operating system, but we don't need to do that with JavaScript. We simply write our code and we hand it over to the web browser, and the web browser takes care all interpreting it and running it on the machine. Now, JavaScript is case sensitive.

Let me repeat that. JavaScript is case sensitive! If you're coming from a case-insensitive language, like Visual Basic or Pascal or even old-school HTML, you may want to write that down and stick it in front of your face for a while, because it's really important. Now this is often a challenge for web designers coming from years of handwriting HTML, where, okay, it's good practice to be consistent, but really if you open a lowercase p tag and close with an uppercase P tag, it doesn't really matter.

Well, it matters here. One will work; the other will not. Mix up the case in a JavaScript keyword and it will not guess. It will not assume what you meant. It just won't work. JavaScript will let you be a little sloppy in some other areas, but case sensitivity is not one of them. Now JavaScript code, like in most programming languages, is grouped into statements, separate instructions or commands to say piece by piece what you want your script to do, perhaps change the color of the piece of text, calculate someone's age, move an image five pixels to the left, make a menu option disappear or appear, or pop up an alert message.

Now what I'm writing here is obviously not JavaScript; it's what's often called pseudo code, just plain English lines of what I might want a script to do without worrying about syntax. A JavaScript statement is typically written by itself on one line. In the same way that you might use a period or a full stop, to finish a sentence in English, each JavaScript statement should end with a semicolon. Now because of this, you can put multiple statements on one line, just separating each of them with the semicolon, but don't do this--it makes it harder to read, split multiple statements onto multiple lines.

In fact, what's more likely is the opposite situation where if you have a really, really, really long statement, you can split that across multiple lines to make it easier to read, marking the end of the full statement with a semicolon, but most of the time one statement, one line. Now, JavaScript is a forgiving language about some things. That might sound like a good thing, but it really isn't. If you're familiar with HTML, you know you can forget to close a tag and most of the time the page would still work, but you know this ends up being a bad thing, because it leads to sloppy code, and it leads to browsers trying to guess what you meant and often different browsers doing different things with that guess.

Now the equivalent here is that in many cases your JavaScript statement would still work if you left off the semicolon, but don't. Let's just imagine it's required, because if you're casual about it, you will run into problems. So we'll always be conscious of using the semicolon to finish a statement. A JavaScript might be case sensitive, but it's not sensitive to spaces or line returns between different pieces of the language. So if I have the statement "Hello world," I could also write it like this with a lot of spaces between the different pieces of the statement.

I could even split it onto different lines. You can typically use however many spaces or line breaks make it readable to you, but the only place spaces do matter is here inside the double quotes, because it then assumes that we want to write out Hello, space, space, space world. But other than this, and other than the idea that you can't put a space in the middle of a word, JavaScript really doesn't care. Now as you're writing code, it's useful to add comments, so you can add comments to your JavaScript by simply typing a line with two forward slashes both together.

This is like other C-based languages. JavaScript than ignores anything after the two forward slashes and begins again on the next line. Now, typically you add comments on the line above the one they're referring to, but you can also add them at the end of statement as well, and they will be ignored there. You can also comment out multiple lines by typing a forward slash asterisk, and everything after that is considered a comment, even if it's a hundred lines later, until you see the flip side, the asterisk forward slash. Now I only tend to use this to comment out large chunks of code when testing; everywhere else I use the single-line style.

It's a bit more obvious. Now like most programming languages, the default behavior when JavaScript runs is to start at the first statement and move down, executing each statement one by one. And the question is, when? And unless you say otherwise, it's as soon as the browser sees your code, and that can have very interesting implications, because we can put the script tags just about anywhere in our HTML. We can put it in the head section, in the body section, in the middle, but let me show you the impact of this.

So again, I'm looking at this very simple, very straightforward HTML page with a little bit of JavaScript in it. I'm going to cut the script tags from where they were and place them in the head section, and you'll see this a lot. You'll see a lot of script tags in the head section. I'm then going to save this and double-click this HTML file to open it up in the browser. Now, notice what it's doing here. The browser is reading our HTML, and it's going line by line through the HTML.

It hits the script tag and is executing this script, this line, as soon as it gets to it. Now that alert is causing us to pause and as you can see, the page is actually blank. It's grayed out, but it's completely blank until I click OK, and then we see the content of the page actually jump in and fill and be rendered. Now if I take that script tag, cut it from the head, and move it to the end, save that, run this again, while we get the same JavaScript behavior, you'll notice that the actual page content has already been rendered before we then ran our JavaScript.

So the position of your JavaScript in your page really does matter, but by default it will be interpreted and executed as soon as the browser sees it. Now, okay, on a simple page like this that's fine, but of course we don't always want to run everything immediately. Okay, sometimes we want to run the code when the page loads, but sometimes not till the user fills out a form, or moves their mouse, or hits the Tab key. And we'll do that, as in most languages, by grouping our code into separate self-contained pieces and then telling the browser we only want to run them in response to certain events, but that's another movie and we've got a couple of things to cover first.

Show transcript

This video is part of

Image for JavaScript Essential Training
JavaScript Essential Training

56 video lessons · 102070 viewers

Simon Allardice
Author

 
Expand all | Collapse all
  1. 3m 28s
    1. Welcome
      1m 1s
    2. What you should know
      1m 44s
    3. Using the exercise files
      43s
  2. 15m 41s
    1. Introduction to JavaScript
      8m 6s
    2. Creating your first JavaScript
      2m 13s
    3. Getting to know the tools and applications
      5m 22s
  3. 56m 8s
    1. Understanding the structure of JavaScript code
      7m 9s
    2. Where to write your JavaScript
      3m 56s
    3. Creating variables
      6m 21s
    4. Working with conditional code
      5m 44s
    5. Working with operators
      13m 28s
    6. Sending messages to the console
      2m 59s
    7. Working with loops
      8m 1s
    8. Creating functions
      8m 30s
  4. 36m 13s
    1. Working with arrays
      7m 57s
    2. Working with numbers
      6m 13s
    3. Working with strings
      8m 27s
    4. Working with dates
      5m 38s
    5. Working with objects
      7m 58s
  5. 9m 6s
    1. What is the DOM?
      5m 49s
    2. Working with nodes and elements
      3m 17s
  6. 25m 17s
    1. Accessing DOM elements
      11m 3s
    2. Changing DOM elements
      5m 42s
    3. Creating DOM elements
      8m 32s
  7. 24m 45s
    1. Introduction to JavaScript event handling
      8m 16s
    2. Working with onClick and onLoad events
      7m 36s
    3. Working with onBlur and onFocus events
      2m 36s
    4. Working with timers
      6m 17s
  8. 21m 41s
    1. Common JavaScript errors
      7m 14s
    2. Using Firebug
      4m 7s
    3. Going through a debugging session
      10m 20s
  9. 10m 13s
    1. Accessing form elements
      4m 20s
    2. Preventing a form from being submitted
      2m 36s
    3. Hiding and showing form sections
      3m 17s
  10. 9m 49s
    1. CSS and JavaScript
      3m 46s
    2. Removing and applying CSS classes
      2m 16s
    3. Changing inline styles
      3m 47s
  11. 19m 44s
    1. Understanding JavaScript style
      7m 39s
    2. Minifying your code
      4m 28s
    3. Using JavaScript code checkers
      7m 37s
  12. 22m 24s
    1. Introduction to JavaScript libraries
      3m 17s
    2. Linking to multiple JavaScript files
      2m 11s
    3. Introduction to jQuery
      12m 7s
    4. Using a content distribution network to deliver JavaScript files
      4m 49s
  13. 17m 35s
    1. JavaScript in HTML5
      9m 37s
    2. Using Modernizr
      3m 2s
    3. Using Strict Mode
      4m 56s
  14. 33m 3s
    1. Knowing the JavaScript to avoid
      6m 35s
    2. Introduction to regular expressions
      6m 56s
    3. Working with AJAX
      10m 44s
    4. Working with objects and prototypes
      8m 48s
  15. 21m 10s
    1. Example: Countdown
      8m 3s
    2. Example: Resize
      5m 47s
    3. Example: Accordion
      7m 20s
  16. 4m 58s
    1. Where to go from here
      4m 0s
    2. Goodbye
      58s

Start learning today

Get unlimited access to all courses for just $25/month.

Become a member
Sometimes @lynda teaches me how to use a program and sometimes Lynda.com changes my life forever. @JosefShutter
@lynda lynda.com is an absolute life saver when it comes to learning todays software. Definitely recommend it! #higherlearning @Michael_Caraway
@lynda The best thing online! Your database of courses is great! To the mark and very helpful. Thanks! @ru22more
Got to create something yesterday I never thought I could do. #thanks @lynda @Ngventurella
I really do love @lynda as a learning platform. Never stop learning and developing, it’s probably our greatest gift as a species! @soundslikedavid
@lynda just subscribed to lynda.com all I can say its brilliant join now trust me @ButchSamurai
@lynda is an awesome resource. The membership is priceless if you take advantage of it. @diabetic_techie
One of the best decision I made this year. Buy a 1yr subscription to @lynda @cybercaptive
guys lynda.com (@lynda) is the best. So far I’ve learned Java, principles of OO programming, and now learning about MS project @lucasmitchell
Signed back up to @lynda dot com. I’ve missed it!! Proper geeking out right now! #timetolearn #geek @JayGodbold
Share a link to this course

What are exercise files?

Exercise files are the same files the author uses in the course. Save time by downloading the author's files instead of setting up your own files, and learn by following along with the instructor.

Can I take this course without the exercise files?

Yes! If you decide you would like the exercise files later, you can upgrade to a premium account any time.

Become a member Download sample files See plans and pricing

Please wait... please wait ...
Upgrade to get access to exercise files.

Exercise files video

How to use exercise files.

Learn by watching, listening, and doing, Exercise files are the same files the author uses in the course, so you can download them and follow along Premium memberships include access to all exercise files in the library.


Exercise files

Exercise files video

How to use exercise files.

For additional information on downloading and using exercise files, watch our instructional video or read the instructions in the FAQ.

This course includes free exercise files, so you can practice while you watch the course. To access all the exercise files in our library, become a Premium Member.

Join now "Already a member? Log in

Are you sure you want to mark all the videos in this course as unwatched?

This will not affect your course history, your reports, or your certificates of completion for this course.


Mark all as unwatched Cancel

Congratulations

You have completed JavaScript Essential Training.

Return to your organization's learning portal to continue training, or close this page.


OK
Become a member to add this course to a playlist

Join today and get unlimited access to the entire library of video courses—and create as many playlists as you like.

Get started

Already a member?

Become a member to like this course.

Join today and get unlimited access to the entire library of video courses.

Get started

Already a member?

Exercise files

Learn by watching, listening, and doing! Exercise files are the same files the author uses in the course, so you can download them and follow along. Exercise files are available with all Premium memberships. Learn more

Get started

Already a Premium member?

Exercise files video

How to use exercise files.

Ask a question

Thanks for contacting us.
You’ll hear from our Customer Service team within 24 hours.

Please enter the text shown below:

The classic layout automatically defaults to the latest Flash Player.

To choose a different player, hold the cursor over your name at the top right of any lynda.com page and choose Site preferencesfrom the dropdown menu.

Continue to classic layout Stay on new layout
Exercise files

Access exercise files from a button right under the course name.

Mark videos as unwatched

Remove icons showing you already watched videos if you want to start over.

Control your viewing experience

Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.

Interactive transcripts

Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.

Are you sure you want to delete this note?

No

Your file was successfully uploaded.

Thanks for signing up.

We’ll send you a confirmation email shortly.


Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

Keep up with news, tips, and latest courses with emails from lynda.com.

Sign up and receive emails about lynda.com and our online training library:

Here’s our privacy policy with more details about how we handle your information.

   
submit Lightbox submit clicked
Terms and conditions of use

We've updated our terms and conditions (now called terms of service).Go
Review and accept our updated terms of service.