Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Javascript

Download as pdf or txt
Download as pdf or txt
You are on page 1of 16

JavaScript

What is JavaScript?
• JavaScript was introduced in 1995 as a way to add programs to web
pages in the Netscape Navigator browser
• JavaScript was initially created to “make web pages alive”
• The programs in this language are called scripts
• They can be written right in a web page’s HTML and executed
automatically as the page loads.
• Scripts are provided and executed as plain text. They don’t need
special preparation or compilation to run.
• In this aspect, JavaScript is very different from another language
called Java.
Why JavaScript?
• 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.
What can in-browser JavaScript do?
• Add new HTML to the page, change the existing content, modify
styles.
• React to user actions, run on mouse clicks, pointer movements, key
presses.
• Send requests over the network to remote servers, download and
upload files (AJAX).
• Get and set cookies, ask questions to the visitor, show messages.
• Remember the data on the client-side (“local storage”).
What CAN’T in-browser JavaScript do?
• 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.
• Different tabs/windows generally do not know about each other
• JavaScript can easily communicate over the net to the server where
the current page came from. But its ability to receive data from other
sites/domains is crippled
What CAN’T in-browser JavaScript do?
• 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.
What makes JavaScript unique?
• Full integration with HTML/CSS.
• Simple things are done simply.
• Support by all major browsers and enabled by default.
Code editors

IDE Lightweight editors


• WebStorm (paid) • Visual Studio Code (cross-
• NetBeans (free) platform, free)
• Visual Studio (paid) • Atom (cross-platform, free)
• Visual Studio Community (free) • Sublime Text (cross-platform,
shareware).
• Notepad++ (Windows, free).
• Vim and Emacs
Variables
• To create a variable in JavaScript, use the let keyword
• Variable naming
• The name must contain only letters, digits, or the symbols $ and _.
• The first character must not be a digit.
• When the name contains multiple words, camelCase is commonly used
• Constants
• To declare a constant (unchanging) variable, use const instead of let
Data Types
• number
• bigint
• string
• boolean
• null
• undefined
• object
• symbol
Conditional Operators
• The “if” statement
let year = prompt('In which year was the ECMAScript-2015 specification
published?', ‘’);
if (year < 2015) {
alert( 'Too early...' );
} else if (year > 2015) {
alert( 'Too late' );
} else {
alert( 'Exactly!' );
}

• Conditional operator ‘?’


let result = condition ? value1 : value2;
Loops
• The “while” loop
while (condition) {
// code // so-called "loop body"
}

• The “do…while” loop


do {
// loop body
} while (condition);

• The “for” loop


for (begin; condition; step) {
// ... loop body ...
}
The "switch" statement
switch(x) {
case 'value1': // if (x === 'value1’)
...
[break]
case 'value2': // if (x === 'value2’)
...
[break]
default:
...
[break]
}
Functions
function name(parameters) {
...body...
}
Interaction
• alert
• shows a message.
• prompt
• shows a message asking the user to input text. It returns the text or, if Cancel
button or Esc is clicked, null.
• confirm
• shows a message and waits for the user to press “OK” or “Cancel”
• It returns true for OK and false for Cancel/Esc.
Developer Console
• http://javascript.info/article/devtools/bug.html

You might also like