Java Script
Java Script
Java Script
JavaScript Where To
The <script> Tag
For example, a function can be called when an event occurs, like when the user
clicks a button.
JavaScript in <head>
JavaScript in <body>
External JavaScript
External scripts are practical when the same code is used in many different web
pages.
To use an external script, put the name of the script file in the src (source)
attribute of a <script> tag.
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
2. JavaScript Output
JavaScript Display Possibilities
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.
3. JavaScript Statements
JavaScript Programs
JavaScript Statements
This statement tells the browser to write "Hello Dolly." inside an HTML
element with id="demo":
The statements are executed, one by one, in the same order as they are written.
Semicolons ;
JavaScript ignores multiple spaces. You can add white space to your script to
make it more readable.
For best readability, programmers often like to avoid code lines longer than 80
characters.
If a JavaScript statement does not fit on one line, the best place to break it is
after an operator.
One place you will find statements grouped together in blocks, is in JavaScript
functions.
JavaScript Keywords
JavaScript statements often start with a keyword to identify the JavaScript
action to be performed.
4. JavaScript Syntax
JavaScript Values
Fixed values
Variable values
JavaScript Literals
The two most important syntax rules for fixed values are:
JavaScript Variables
JavaScript uses the keywords var, let and const to declare variables.
JavaScript Operators
JavaScript Keywords
JavaScript Comments
The rules for legal names are the same in most programming languages.
Unicode covers (almost) all the characters, punctuations, and symbols in the
world.
5. JavaScript Variables
Variables are Containers for Storing Data
JavaScript Variables can be declared in 4 ways:
Automatically
Using var
Using let
Using const
JavaScript Identifiers
Identifiers can be short names (like x and y) or more descriptive names (age,
sum, totalVolume).
The general rules for constructing names for variables (unique identifiers) are:
JavaScript variables can hold numbers like 100 and text values like "John
Doe".
JavaScript can handle many types of data, but for now, just think of numbers
and strings.
Strings are written inside double or single quotes. Numbers are written without
quotes.
You declare a JavaScript variable with the var or the let keyword:
Value = undefined
In computer programs, variables are often declared without a value. The value
can be something that has to be calculated, or something that will be provided
later, like user input.
The variable carName will have the value undefined after the execution of this
statement.
If you re-declare a JavaScript variable declared with var, it will not lose its
value.
The variable carName will still have the value "Volvo" after the execution of
these statements.
Since JavaScript treats a dollar sign as a letter, identifiers containing $ are valid
variable names.
6. JavaScript Operators
There are different types of JavaScript operators:
Arithmetic Operators
Assignment Operators
Comparison Operators
String Operators
Logical Operators
Bitwise Operators
Ternary Operators
Type Operators