Javascript
Javascript
Javascript
INTRODUCTION TO
JAVASCRIPT
WHAT IS JAVASCRIPT?
• JavaScript was designed to add interactivity to HTML
pages
• JavaScript is a scripting language (a scripting language is
a lightweight programming language)
• A JavaScript consists of lines of executable computer
code
• JavaScript is an interpreted language (means that scripts
execute without preliminary compilation)
• Everyone can use JavaScript without purchasing a
Are Java and JavaScript the Same?
•NO!
•Java and JavaScript are two completely different
languages in both concept and design!
•Java is a powerful and much more complex
programming language - in the same category as
C and C++.
Ending Statements With a Semicolon?
•With traditional programming languages, like C++
and Java, each code statement has to end with a
semicolon (;).
•Many programmers continue this habit when
writing JavaScript, but in general, semicolons are
optional! However, semicolons are required if you
want to put more than one statement on a single
line.
A JavaScript variable is simply a name of
storage location. There are two types of
variables in JavaScript : local variable and
global variable.
There are some rules while declaring a
JavaScript variable (also known as
identifiers).
1.Name must start with a letter (a to z or A to Z), underscore( _ ),
or dollar( $ ) sign.
2.After first letter we can use digits (0 to 9), for example value1.
3.JavaScript variables are case sensitive, for example x and X are
different variables.
1.var x = 10;
2. var 123=30;
3.var _value="sonoo";
4. var *aa=320;
VARIABLES IN JAVASCRIPT
var, const, let
• var — The most common variable. Can be reassigned
but only accessed within a function. Variables defined
with var move to the top when code is executed.
• const — Can not be reassigned and not accessible
before they appear within the code.
• let — Similar to const, however, let variable can be
reassigned but not re-declared.
JavaScript local variable
A JavaScript local variable is declared
inside block or function. It is accessible
within the function or block only. For
example:
<script>
function abc(){
var x=10;//local variable
}
</script>
JavaScript global variable
A JavaScript global variable is accessible from any
function. A variable i.e. declared outside the function or
declared with window object is known as global variable.
For example:
<script>
var data=200;//gloabal variable
function a(){
document.writeln(data);
}
function b(){
document.writeln(data);
}
a();//calling JavaScript function
b();
</script>
Data Types
• Numbers — var age = 23
• Variables — var x
• Text (strings) — var a = “ste"
• Operations — var b = 1 + 2 + 3 3 of 24
• True or false statements — var c = true
• Constant numbers — const PI = 3.14
• Objects — var name = {firstName:"John",
lastName:”Doe"}
JavaScript Operators
•Basic Operators
+ — Addition
- — Subtraction
* — Multiplication
/ — Division
(...) — Grouping operator, operations within brackets are executed earlier than
those outside
% — Modulus (remainder )
++ — Increment numbers
-- — Decrement numbers Operators
Operator Description Example Result
+ Addition x=2 4
y=2
x+y
- Subtraction x=5 3
x-y
* Multiplication x=5 20
y=4
x*y
/ Division 15/5 3
5/2 2,5
10%8 2
10%2 0
x++
x--
Comparison Operators
== — Equal to
=== — Equal value and equal type
!= — Not equal
!== — Not equal value or not equal type
>— Greater than
< — Less than
>= — Greater than or equal to
<= — Less than or equal to
? — Ternary operator
Operator Description Example
y="5"
Comparison Operators
x==y returns true
= x=y x=y
Assignment Operators
+= 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
Operator Description Example
y=3
Operators
|| or x=6
y=3
! not x=6
y=3
<head>
<script > <body>
function msg(){ <p>Welcome to Javascript</p>
alert("Hello Hawking"); <form>
} <input type="button" value="click" onclick="msg()"/>
</script> </form>
</head> </body>
3. External JavaScript file
We can create external JavaScript file and embed it in many html page.
It provides code re usability because single JavaScript file can be used in
several html pages.
An external JavaScript file must be saved by .js extension. It is
recommended to embed all JavaScript files into a single file. It increases
the speed of the webpage.
Statement:
document.getElementById(“sample”).innerHTML=“Hello
World.”;
Output:
Hello World