Java script syllabus
Java script syllabus
What is JavaScript?
Netscape Communications Corporation developed JavaScript in 1995.
Brendan Eich, a software engineer at Netscape, created the language
JavaScript is dynamically typed, which means that the type of a variable is
determined at runtime, not at compile time.
● JavaScript is the programming language of the web.
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').innerHTML = Date()">
<p id="demo"></p>
</body>
</html>
# The example below "finds" an HTML element (with id="demo"), and changes
the element content (innerHTML) to "Hello JavaScript":
<html>
<body>
</body>
</html>
<html>
<body>
<button type="button"
onclick="document.getElementById('demo').style.fontSize='35px'">Click Me!</button>
3
</body>
</html>
<html>
<body>
<p id="demo"></p>
<script>
</script>
</body>
</html>
For example, a function can be called when an event occurs, like when the user
clicks a button.
Scripts can be placed in the <body>, or in the <head> section of an HTML page,
or in both.
<html>
4
<head>
<script>
function myFunction() {
</script>
</head>
<body>
</body>
</html>
<html>
<body>
<script>
function myFunction() {
</script>
</body>
</html>
5
JavaScript Output
JavaScript can "display" data in different ways:
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:
#Example
<html>
<body>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = 5 + 6;
</script>
</body>
</html>
#using document.write
<html>
<body>
6
<script>
document.write(5 + 6);
</script>
</body>
</html>
#window.alert()
<html>
<body>
<script>
window.alert(5 + 6);
</script>
</body>
</html>
Or
<html>
<body>
<script>
alert(5 + 6);
7
</script>
</body>
</html>
JavaScript Syntax
// How to create variables:
var x;
let y;
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
8
JavaScript uses the keywords var, let and const to declare variables.
<html>
<body>
<h2>JavaScript Variables</h2>
<p id="demo"></p>
<script>
let x;
x = 6;
document.getElementById("demo").innerHTML = x;
</script>
</body>
</html>
JavaScript Keywords
JavaScript keywords are used to identify actions to be performed.
The rules for legal names are the same in most programming languages.
● Or an underscore (_)
JavaScript Comments
JavaScript comments can be used to explain JavaScript code, and to make it
more readable.
Any text between // and the end of the line will be ignored by JavaScript (will
not be executed).
Multi-line Comments
Multi-line comments start with /* and end with */.
JavaScript Variables
10
● Automatically
● Using var
● Using let
● Using const
3. Always use const if the type should not be changed (Arrays and Objects)
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.
Start the statement with let and separate the variables by comma:
Or
carName = "Volvo",
price = 200;
JavaScript Operators
Javascript operators are used to perform different types of mathematical and
logical computations.
Examples:
The Assignment Operator = assigns values
The Addition Operator + adds values
The Multiplication Operator * multiplies values
The Comparison Operator > compares values
● Arithmetic Operators
● Assignment Operators
● Comparison Operators
● String Operators
● Logical Operators
● Bitwise Operators
● Ternary Operators
● Type Operators
Arithmetic Operators
Operator Description
+ Addition
12
- Subtraction
* Multiplication
** Exponentiation
/ Division
++ Increment
– Decrement
Assignment Operators
Operator Example Same As
= x=y x=y
+= x += y x=x+y
-= x -= y x=x-y
*= x *= y x=x*y
/= x /= y x=x/y
%= x %= y x=x%y
**= x **= y x = x ** y
13
Operat Description
or
== equal to
!= not equal
? ternary operator
Operat Description
or
|| logical or
! logical not
14
objects, arrays, dates, maps, sets, intarrays, floatarrays, promises, and more.
JavaScript Functions
A JavaScript function is a block of code designed to perform a particular task.
Function names can contain letters, digits, underscores, and dollar signs (same
rules as variables).
Function arguments are the values received by the function when it is invoked.
Inside the function, the arguments (the parameters) behave as local variables.
15
Function Invocation
The code inside the function will execute when "something" invokes (calls) the
function:
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute
the code after the invoking statement.
Functions often compute a return value. The return value is "returned" back to
the "caller":
Why Functions?
With functions you can reuse code
You can use the same code with different arguments, to produce different
results.
<html>
<body>
<h1>JavaScript Functions</h1>
<p id="demo"></p>
<script>
16
function toCelsius(f) {
document.getElementById("demo").innerHTML = value;
</script>
</body>
</html>
Local Variables
Variables declared within a JavaScript function, become LOCAL to the function.
Since local variables are only recognized inside their functions, variables with
the same name can be used in different functions.
Local variables are created when a function starts, and deleted when the
function is completed.
eval() function:
The eval() method evaluates or executes an argument.
With eval(), malicious code can run inside your application without permission.
17
With eval(), third-party code can see the scope of your application, which can
lead to possible attacks.
Syntax
eval(string)
JavaScript Strings
Strings are for storing text
Using Quotes
A JavaScript string is zero or more characters written inside quotes.
Example:-
Example
The charAt() method returns the character at a specified index (position) in a string:
Example
slice(start, end)
substring(start, end)
substr(start, length)
slice() extracts a part of a string and returns the extracted part in a new string.
The method takes 2 parameters: start position, and end position (end not included).
Example
The difference is that start and end values less than 0 are treated as 0 in substring().
Example
If you omit the second parameter, substring() will slice out the rest of the string.
Example
The concat() method can be used instead of the plus operator. These two lines do the
same:
Example
Examples
An alert box is often used if you want to make sure information comes through to the
user.
When an alert box pops up, the user will have to click "OK" to proceed.
Syntax
window.alert("sometext");
<html>
<body>
<h2>JavaScript Alert</h2>
<script>
function myFunction() {
</script>
</body>
</html>
22
Confirm Box
A confirm box is often used if you want the user to verify or accept something.
When a confirm box pops up, the user will have to click either "OK" or "Cancel" to
proceed.
If the user clicks "OK", the box returns true. If the user clicks "Cancel", the box returns
false.
Syntax
window.confirm("sometext");
Example
if (confirm("Press a button!")) {
} else {
Prompt Box
A prompt box is often used if you want the user to input a value before entering a page.
When a prompt box pops up, the user will have to click either "OK" or "Cancel" to
proceed after entering an input value.
If the user clicks "OK" the box returns the input value. If the user clicks "Cancel" the box
returns null.
23
Syntax
window.prompt("sometext","defaultText");
Example
let text;
} else {
JavaScript Objects
Real Life Objects
In real life, objects are things like: houses, cars, people, animals, or any other
subjects.
Object Properties
A real life car has properties like weight and color:
Car objects have the same properties, but the values differ from car to car.
Object Methods
A real life car has methods like start and stop:
24
Car objects have the same methods, but the methods are performed at different
times.
JavaScript Variables
JavaScript variables are containers for data values.
example
JavaScript Objects
Objects are variables too. But objects can contain many values.
This code assigns many values (Fiat, 500, white) to an object named car: