Sunteți pe pagina 1din 13

Introduction

to
JavaScript
About JavaScript

• JavaScript is a programming language.


• JavaScript is Object Based Programming language NOT Object
oriented.
• JavaScript and JAVA are not same.
• JavaScript is weakly typed, Client side Interpreted Language.
• Designed by Brendan Eich for Netscape.
• It was originally called Mocha, renamed to LiveScript, and then
renamed to JavaScript. the official standard is just called ECMAScript.
What can do using JavaScript?
• JavaScript Can Change HTML Content

<!DOCTYPE html>
<html>
<body>

<p id="demo">JavaScript can change HTML content.</p>

<button type="button" onclick='document.getElementById("demo").innerHTML = “Content Changed!"'>


Click Me!</button>

</body>
</html>
• JavaScript Can Change HTML Attribute Values

<!DOCTYPE html>
<html>
<body>

<div id="target" title="box" style="width:100px; height:100px; background:green"></div>

<button onclick="document.getElementById('target').title='Box A'">A </button>


<button onclick="document.getElementById('target').title='Box B'">B </button>

</body>
</html>
• JavaScript Can Change HTML Styles (CSS)

<!DOCTYPE html>
<html>
<body>
<p id="demo">JavaScript can change the style of an HTML element.</p>

<button type="button" onclick="document.getElementById('demo').style.color=‘red'">Click Me!</button>

</body>
</html>

<button type="button" onclick="document.getElementById('demo').setAttribute("style", "font-size: 100px;


font-style: italic; color:#ff0000;");'">Click Me!</button>
• JavaScript Can Hide HTML Elements

<!DOCTYPE html>
<html>
<body>

<p id="demo">JavaScript can hide HTML elements.</p>

<button type="button" onclick="document.getElementById('demo').style.display='none'">Click Me!


</button>

</body>
</html>
• JavaScript Can Show HTML Elements

<!DOCTYPE html>
<html>
<body>

<p id="demo" style="display:none">Hello JavaScript!</p>

<button type="button" onclick="document.getElementById('demo').style.display='block'">Click Me!


</button>

</body>
</html>
How to use JavaScript?
• Inline
<button type="button" onclick="document.getElementById('demo').style.display='none'">Click
Me!</button>

• Internal
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>

• External
<script src="myScript.js"></script>
JavaScript Display:

• Writing into an HTML element, using innerHTML.


<p id="demo"></p>
<script>document.getElementById("demo").innerHTML = 5 + 6;</script>
• Writing into the HTML output using document.write().

<script>document.write("Hello World");</script>

• Writing into an alert box, using window.alert().

<script>window.alert(5 + 6);</script>

• Writing into the browser console, using console.log().

<script>console.log(5 + 6);</script>
JavaScript Statements

JavaScript statements are composed of: Expressions, Values, Operators, Keywords, and
Comments.
1. JavaScript variables are containers for storing data values. In this example, x, y, and z, are variables:
var name = “CodersTrust”;
var age = 5;
var x = 10;
var y = 20;
var z = x + y ;
var person = "John Doe", carName = "Volvo", price = 200;

Naming Convention of Variable :

• Names can contain letters, digits, underscores, and dollar signs.


• Names must begin with a letter
• Names can also begin with $ and _ (but we will not use it in this tutorial)
• Names are case sensitive (y and Y are different variables)
• Reserved words (like JavaScript keywords) cannot be used as names
JavaScript Arithmetic Operators JavaScript Assignment Operators

Operator Description Operator Example Same As


+ Addition = x=y x=y
- Subtraction
+= x += y x=x+y
* Multiplication
/ Division -= x -= y x=x-y
% Modulus (Division Remainder) *= x *= y x=x*y
++ Increment
/= x /= y x=x/y
-- Decrement
%= x %= y x=x%y

JavaScript Logical Operators


Operato Description
r
&& logical and
|| logical or
! logical not
JavaScript Comparison Operators
Operator Description
== 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
Data types :
The latest ECMAScript standard defines seven data types:
Six data types that are primitives:
• Boolean (E.g.) var x = true; var y = false;
• Null (E.g) var x = null; <script type="text/javascript">
• Undefined (E.g.) var x; var marks = '60';
• Number (E.g.) var x = 10; switch (marks)
• String (E.g.) var x = “I want to learn JavaScript”; {
• Object case '80': document.write("A+");
break;
Conditional Statements
<script> case '70': document.write("A");
var number= 10; break;
if (number%2 == 0){
document.write("Even Number"); case '60': document.write("A-");
} break;
else {
document.write("Odd Number"); default: document.write("Fail")
} }
</script> </script>

S-ar putea să vă placă și