ITEW2 WEEK 1 Introduction To JavaScript and JavaScript Fundamentals Part 1
ITEW2 WEEK 1 Introduction To JavaScript and JavaScript Fundamentals Part 1
COURSE INTENDED 1. Design and implement JavaScript codes appropriate to the requirements of the
LEARNING OUTCOMES: programming problem.
2. Write readable JavaScript programs compliant to widely-accepted coding
conventions implementing OOP approach.
3. Create JavaScript programs applying JQuery and Web Storage.
LEARNING MATERIAL
FOR WEEK NUMBER:
1
I. TITLE: INTRODUCTION TO JAVASCRIPT AND JAVASCRIPT FUNDAMENTALS PART 1
III. INTRODUCTION: This lesson will cover topics on the introduction to JavaScript as a scripting language
for front-end web development. The use of the “script” tag, code structure, and the
modern mode to client side scripting will be clearly explained as well some simple
examples of JavaScript code.
JavaScript contains a standard library of objects, such as Array, Date, and Math, and
a core set of language elements such as operators, control structures, and statements.
Core JavaScript can be extended for a variety of purposes by supplementing it with
additional objects; for example:
This means that in the browser, JavaScript can change the way the webpage (DOM)
looks. And, likewise, Node.js JavaScript on the server can respond to custom requests
from code written in the browser [1].
IV. CONTENTS:
Lesson Coverage:
1. What is JavaScript?
2. Hello, world!
3. Code Structure
4. The modern mode, "use strict"
What is JavaScript?
o Scripts can be written right in a web page’s HTML and run automatically as the page loads.
o Scripts are provided and executed as plain text (no special preparation or compilation to run).
• Today, JavaScript can execute not only in the browser, but also on the server, or actually on any device that has a
special program called the JavaScript engine.
o The browser has an embedded engine sometimes called a “JavaScript virtual machine”. Different engines
have different “codenames”. For example:
o There are other codenames like “Chakra” for IE, “ChakraCore” for Microsoft Edge, “Nitro” and “SquirrelFish”
for Safari, etc.
When JavaScript was created, it initially had another name: “LiveScript”. But Java was very popular at that
time, so it was decided that positioning a new language as a “younger brother” of Java would help.
But as it evolved, JavaScript became a fully independent language with its own specification
called ECMAScript, and now it has no relation to Java at all.
Youtube Video
Modern JavaScript is a “safe” programming language. It does not provide low-level access to memory or CPU,
because it was initially created for browsers which do not require it.
JavaScript’s capabilities greatly depend on the environment it’s running in. For instance, Node.js supports
functions that allow JavaScript to read/write arbitrary files, perform network requests, etc.
In-browser JavaScript can do everything related to webpage manipulation, interaction with the user, and the
webserver.
• Add new HTML to the page, change the existing content, and modify styles.
• React to user actions, run on mouse clicks, pointer movements, and key presses.
• Send requests over the network to remote servers, download and upload files (so called AJAX and COMET
technologies).
• Validate form data entered by the user.
• Get and set cookies, ask questions to the visitor, show messages.
• Remember the data on the client-side (“local storage”).
JavaScript’s abilities in the browser are limited for the sake of the user’s safety. The aim is to prevent an evil
webpage from accessing private information or harming the user’s data.
• JavaScript on a webpage may not read/write arbitrary files on the hard disk, copy them or execute
programs. It has no direct access to OS system functions.
• JavaScript from one page may not access the other if they come from different sites (from a different
domain, protocol or port).
• JavaScript ability to receive data from other sites/domains is crippled (Though possible, it requires
explicit agreement (expressed in HTTP headers) from the remote side.)
• Such limits do not exist if JavaScript is used outside of the browser, for example on a server. Modern
browsers also allow plugin/extensions which may ask for extended permissions.
Hello, world!
This part of the lesson is about core JavaScript, the language itself.
But we need a working environment to run our scripts and, since this book is online, the browser is a good
choice. We’ll keep the amount of browser-specific commands (like alert) to a minimum so that you don’t spend time
on them if you plan to concentrate on another environment (like Node.js). We’ll focus on JavaScript in the browser
in the next part of the lesson.
JavaScript programs can be inserted into any part of an HTML document with the help of the <script> tag.
For instance:
The <script> tag contains JavaScript code which is automatically executed when the browser processes the
tag.
Modern markup
The <script> tag has a few attributes that are rarely used nowadays but can still be found in old code:
The old HTML standard, HTML4, required a script to have a type. Usually it was type="text/javascript". It’s
not required anymore. Also, the modern HTML standard, HTML5, totally changed the meaning of this
attribute. Now, it can be used for JavaScript modules. But that’s an advanced topic; we’ll talk about modules
in another part of the tutorial.
This attribute was meant to show the language of the script. This attribute no longer makes sense because
JavaScript is the default language. There is no need to use it.
In really ancient books and guides, you may find comments inside <script> tags, like this:
This trick isn’t used in modern JavaScript. These comments hide JavaScript code from old browsers that
didn’t know how to process the <script> tag. Since browsers released in the last 15 years don’t have this issue, this
kind of comment can help you identify really old code.
External scripts
If we have a lot of JavaScript code, we can put it into a separate file. Script files are attached to HTML with
the src attribute:
Here, /path/to/script.js is an absolute path to the script file (from the site root). You can also provide a
relative path from the current page. For instance, src="script.js" would mean a file "script.js" in the current folder.
Please Note:
• As a rule, only the simplest scripts are put into HTML. More complex ones reside in separate files.
• The benefit of a separate file is that the browser will download it and store it in its cache.
• Other pages that reference the same script will take it from the cache instead of downloading it, so the file is
actually downloaded only once.
• That reduces traffic and makes pages faster [3].
Code structure
Statements
Statements are syntax constructs and commands that perform actions. We’ve already seen a statement,
alert('Hello, world!'), which shows the message “Hello, world!”. We can have as many statements in our code as we
want. Statements can be separated with a semicolon.
alert('Hello'); alert('World');
Usually, statements are written on separate lines to make the code more readable:
alert('Hello');
alert('World');
Semicolons
A semicolon may be omitted in most cases when a line break exists. This would also work:
alert('Hello')
alert('World')
Comments
As time goes on, programs become more and more complex. It becomes necessary to add comments which
describe what the code does and why.
Comments can be put into any place of a script. They don’t affect its execution because the engine simply
ignores them.
The rest of the line is a comment. It may occupy a full line of its own or follow a statement. Like here:
Multiline comments start with a forward slash and an asterisk /* and end with an asterisk and a forward
slash */
The content of comments is ignored, so if we put code inside /* … */, it won’t execute. Sometimes it can be
handy to temporarily disable a part of code:
Please, don’t hesitate to comment your code. Comments increase the overall code footprint, but that’s not a
problem at all. There are many tools which minify code before publishing to a production server. They remove
comments, so they don’t appear in the working scripts. Therefore, comments do not have negative effects on
production at all [4].
For a long time, JavaScript evolved without compatibility issues. New features were added to the language
while old functionality didn’t change. That had the benefit of never breaking existing code. But the downside was
that any mistake or an imperfect decision made by JavaScript’s creators got stuck in the language forever.
This was the case until 2009 when ECMAScript 5 (ES5) appeared. It added new features to the language and
modified some of the existing ones. To keep the old code working, most modifications are off by default. You need to
explicitly enable them with a special directive: "use strict".
“use strict”
The directive looks like a string: "use strict" or 'use strict'. When it is located at the top of a script, the whole
script works the “modern” way. For example:
Quite soon we’re going to learn functions (a way to group commands), so let’s note in advance that "use
strict" can be put at the beginning of a function. Doing that enables strict mode in that function only. But usually
people use it for the whole script.
1. The "use strict" directive switches the engine to the “modern” mode, changing the behavior of some built-in
features.
2. Strict mode is enabled by placing "use strict" at the top of a script or function. Several language features, like
“classes” and “modules”, enable strict mode automatically.
3. Strict mode is supported by all modern browsers.
4. We recommended always starting scripts with "use strict" [5].
DISCLAIMER
Every reasonable effort is made to ensure the accuracy of the information used in the creation of this
reference material, without prejudice to the existing copyrights of the authors. As an off-shoot of the innumerable
difficulties encountered during these trying times, the authors endeavored to ensure proper attribution of the
esteemed original works, by way of footnotes or bibliography, to their best abilities and based on available resources,
despite the limited access and mobility due to quarantine restrictions imposed by the duly constituted authorities.
COPYRIGHT NOTICE
Materials contained in the learning packets have been copied and conveyed to you by or on behalf of
Pamantasan ng Cabuyao pursuant to Section IV - The Copyright Act (RA) 8293 of the Intellectual Property Code of
the Philippines.
You are not allowed by the Pamantasan ng Cabuyao to reproduce or convey these materials. The content may
contain works which are protected by copyright under RA 8293. You may be liable to copyright infringement for any
copying and/ or distribution of the content and the copyright owners have the right to take legal action against such
infringement.