Show
We’re moving into the wild and wonderful world of JavaScript programming. Before we start trying to be a skilled Ninja, we need to first spend some time in the dojo to hone our techniques. In other words, we won’t try to run before we first learn to walk. In this episode, we’ll take a look at the structure of the JavaScript language. Let’s jump right in! JavaScript is written in Plain TextMuch like you understand already from writing HTML and CSS, JavaScript is simply written in plain text. This would be perfectly valid JavaScript in an HTML page:
JavaScript is an Interpreted LanguageThat’s right folks, it’s interpreted, not compiled. This does not make it any less of a programming language much to the chagrin of some of the embedded machine code engineers of the world! Unlike a language like C which must make use of an intermediate program called a compiler to transform high level syntax into machine level code, JavaScript is simply interpreted by the JavaScript engine of the browser at runtime. Modern day JavaScript engines are works of genius, allowing extremely fast and accurate execution of JavaScript code. Therefore, there is no need to worry about JavaScript’s interpreted nature. JS is Case SensitiveIn working with technology, we web developers are exposed to numerous different languages and syntax. Some of the languages we encounter are case insensitive, but JavaScript is case sensitive so make sure to keep this in mind. You will find things breaking and not working with simple case errors. For example alert(‘I am the JavaScript Victim’); works fine whereas Alert(‘I am the JavaScript Victim’); will fail in spectacular fashion. Actually, it won’t be all that spectacular, in fact, the only thing that would tip you off that something went wrong is, nothing would happen. Such is the nature of JavaScript, it is a tricky language to debug, but more on that in a later lesson. JavaScript Runs on StatementsJust like other languages, JavaScript runs one statement at a time. A statement is a group of executable language components terminated by a semicolon. Statements are usually executed in order from top to bottom. The order of execution can be changed by conditional statements such as if and switch, by looping statements such as while, for, and do, and by disruptive statements like a break, return, and throw. Function execution also can change the top to bottom nature of statement execution. JavaScript’s Forgiving Nature is a WeaknessJavaScript is a powerful and capable language. It does have some shortcomings that were actually meant to be helpful features when the language was first conceived. The most popular of them all is the highly infamous semicolon insertion. Built into JavaScript is the capability to detect poor syntax and automatically insert missing semicolons. That sounds like a good idea, right? Wrong. Should you forget to manually insert semicolons at the proper location in your code, Chuck Norris will unleash a fury of karate kicks and nunchuck whipping upon you. Well, maybe that won’t happen, but your scripts will be riddled with bugs that you will have almost no hope of finding, so make sure to just insert your semicolons so that everybody, including yourself, will stay happy. Whitespace is Semi Insensitive in JavaScriptWhat? What the heck is semi insensitive? Well, for the most part, whitespace does not matter in JavaScript. This is why you see such widespread use of minification tools that take human-readable JavaScript syntax code and strip out mostly all of the whitespaces, leading to one long string of JavaScript that looks like some strange form of hieroglyphics. There are instances where whitespace does matter. For example:
The space between var and chimmy is actually needed here for the script to work properly. Or consider:
Whitespace contained within a string also cannot be removed. Comments in JavaScriptJavaScript comments work just like they do in most other C-based languages. You can make use of the well-known multi-line and or single line comment techniques. As you likely know, The Order of Code Execution Matters
Note that we get the same JavaScript alert on that page. Do you notice something different though? That’s right if you look at the background, absolutely no HTML has been rendered yet, and we are frozen in this state until we click ok. This shows that the order of execution is indeed important. This is an overly simple example, but you would be surprised as you move along in your JavaScript journey when you are debugging tricky problems to find out that simply moving your code up or down in the hierarchy of the DOM may solve your issue. It may seem trivial, but it does make a difference. Where To Place JavaScript
Use of the Script TagIn the past, we had to worry about specifying many attributes for the script tag. With the onset of HTML5 and modern browsers, simply including the src attribute is enough.
Where should I place the Script Tags?The script tags can go anywhere on the page, but as a best practice, many developers will place it just before the closing body tag on the HTML page. This provides faster speed load times for your web page. ConclusionThis was a very gentle primer of what’s to come. It has been helpful to review some of the key points and overview of the language. What is the structure of JavaScript program?JavaScript statements are commands to the browser JavaScript code is a sequence of statements JavaScript statements are separated with semicolon Multiple statement on one line is allowed JavaScript statements can be grouped together in code blocks You can break a code line after an operator or a comma.
Is JavaScript a structural language?JavaScript is an structured and dynamic scripting language. The basic syntax was intentionally made similar to both C++ and Java to facilitate professional's and newbie's learning process.
What is the structure of a code?Structure. A Structure is one of the 5 data types in programming. A structure is used to represent information about something more complicated than a single number, character, or boolean can do (and more complicated than an array of the above data types can do).
How many data structures are there in JavaScript?8 Common Data Structures in JavaScript.
|