Java Script
Java Script
SUMBER W3SCHOOL
RIRIN SAFITRI
SEMANGAT BELAJAR!
A. Home
Example:
B. Introduction
Java Script Introduction
The example below "finds" an HTML element (rith id="demo"), and changes the element
content (innerHTML) to "Hello JavaScript";
JavaScript accepts both double and single quotes
Example. Single is (') and double is (")
C. JavaScript Where To
JavaScript Where To
The <script> Tag
In HTML, JavaScript code is inserted between <script> and </script> tags.
Example
JavaScript in <head>
In this example, a JavaScript function is placed in the <head> section of an HTML page.
JavaScript in <body>
In this example, a JavaScript function is placed in the <body> section of an HTML page.
Placing scripts at the buttom of the <body> element improves the display speed
because script interpretation slows down the display
Eksternal JavaScript
Script can also be placed in external files:
External scripts are prectical when the same code is used in many different web pages.
JavaScript file have the file extension .js.
To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag:
Example
You can place an external script reference in <head> or <body> as you like.
The script will behave as if it was located exactly where the <script> tag is located.
To add several script files to one page - use several script tags:
External References
D. JavaScript Output
1. Using innerHTML
To access an HTML element, JavaScript can use the document.getElementById(id) method.
The id attribute defines the HTML element. The innerHTML property defines the HTML
content:
2. Using document.write()
For testing purposes, it is convenient to use document.write():
Using document.write() after an HTML document is loaded, will delete all
existing HTML:
3. Using window.alert()
You can use an alert box to display data:
In JavaScript, the window object is the global scope object. This means that variables,
properties, and methods by default belong to the window object. This also means that
specifying the window keyword is optional:
4. Using console.log()
For debugging purposes, you can call the console.log() method in
the browser to display data.
You will learn more about debugging in a later chapter.
JavaScript Print
The only exception is that you can call the window.print() method in the browser to print the
content of the current window.
E. JavaScript Statements
JavaScript Programs
JavaScript Statements
JavaScript statements are composed of:
Values, Operators, Expressions, Keywords, and Comments.
This statement tells the browser to write "Hello Dolly." inside an HTML element with
id="demo":
Semicolons ;
Semicolons separate JavaScript statements.
Add a semicolon at the end of each executable statement:
When separated by semicolons, multiple statements on one line are allowed:
One place you will find statements grouped together in blocks, is in JavaScript
functions:
F. JavaScript Statements
JavaScript syntax is the set of rules, how JavaScript programs are constructed:
JavaScript Values
The JavaScript syntax defines two types of values:
Fixed values
Variable values
JavaScript Literals
The two most important syntax rules for fixed values are:
JavaScript Variables
In a programming language, variables are used to store data values.
JavaScript uses the keywords var, let and const to declare variables.
JavaScript Operators
JavaScript uses arithmetic operators ( + - * / ) to compute values:
JavaScript Expressions
An expression is a combination of values, variables, and operators, which computes to a
value.
JavaScript Keywords
JavaScript keywords are used to identify actions to be performed.
You will learn more about var and let later in this tutorial.
JavaScript Comments
Not all JavaScript statements are "executed".
The rules for legal names are the same in most programming languages.
Note
Numbers are not allowed as the first character in names.
Hyphens:
Hyphens are not allowed in JavaScript. They are reserved for subtractions.
Underscore:
JavaScript programmers tend to use camel case that starts with a lowercase
letter:
G. JavaScript Comments
Any text between // and the end of the line will be ignored by JavaScript (will
not be executed).
This example uses a single-line comment before each code line:
This example uses a single line comment at the end of each line to explain
the code:
Multi-line Comments
Multi-line comments start with /* and end with */.
H. Variables
Automatically
Using var
Using let
Using const
Note
The var keyword was used in all JavaScript code from 1995 to 2015.
The var keyword should only be used in code written for older browsers.
Mixed Example
The two variables price1 and price2 are declared with the const keyword.
These are constant values and cannot be changed.