What is JavaScript?
JavaScript is a “safe” programming language and does not provide low-level access to memory or CPU, because it was initially created for browsers which do not require it. It is interpreted programming language and designed for creating network-centric applications. JavaScript is a case-sensitive language.
JavaScript - Syntax
We can place the <script> tags, containing JavaScript, anywhere within web page, but it is normally recommended that should keep it within the <head> tags.<script language = "javascript" type = "js/javascript">
JavaScript code
----------------
</script>
We can also link external javascript file using following link under <head> tag.
<script type="text/javascript" src="javascripts.js"></script>
JavaScript Output statements:
We can "display" data in JavaScript in following different ways:
1. Using innerHTML: We can access an HTML element using document.getElementById(id) method.
The id attribute defines the HTML element and innerHTML property defines the content.
Example
<HTML> <BODY> <button onclick="document.getElementById('myImage').src='picts/ontorch.jpg'">Turn on the Torch</button> <img id="myImage" src="picts/ontorch.jpg" style="width:100px"> <button onclick="document.getElementById('myImage').src='picts/offtorch.jpg'">Turn off the Torch</button> </BODY> </HTML>
Output
2. Using document.write(): Display message or value on document.
Example 1
<html> <body> <script language = "javascript" type = "text/javascript"> <!-- document.write("Hello World!") //--> </script> </body> </html>
Hello World!
Example 2
<button type="button" onclick="document.write(5 + 6)">Try it</button>3. Using alert(): Display message or value in alert box pops up, the user will have to click "OK" to proceed..
Example
<button type="button" onclick="alert(5 + 6)">Try it</button>JavaScript Print:
To prin document in javascript we can call the window.print() method in the browser to print the content of the current window.
Example
<button type="button" onclick="window.print()">Print this page</button>Comments in JavaScript:
Any text between a // and the end of a line, /* and */, <!-- is treated as a comment and is ignored by JavaScript.
JavaScript Input statements:
parseInt(prompt()) Method:
Display a prompt box which ask the user to input value.
Example Q JavaScript Datatypes:
JavaScript allows us to work with three primitive data types:−
Numbers, eg. 123, 120.50 etc.
Strings of text e.g. "This text string" etc.
Boolean e.g. true or false.
JavaScript Variables:
We must declare variable in a JavaScript program. Variables are declared with the var keyword.
Syntax
<script type = "text/javascript">
var m,n;
</script>Using an External JavaScript:
If we want to run the same JavaScript on several pages, without having to write the same script on every page, we can write a JavaScript in an external file. To use the external script, point to the .js file in the "src" attribute of the tags!
Example
<html> <head> <script type="text/javascript" src="myscript.js"></script> </head> <body> </body> </html>
Data Types:
There are two types of data types in JavaScript which provides to hold different types of values.
- Primitive data type
- Non-primitive (reference) data type
Example
var a=50;//Holding number var nm="Rakesh";//Holding string
Primitive data types
There are five types of primitive data types in JavaScript. They are as follows:
| Data Type | Description |
|---|---|
| String | represents sequence of characters e.g. "hello" |
| Number | represents numeric values e.g. 100 |
| Boolean | represents boolean value either false or true |
| Undefined | represents undefined value |
| Null | represents null i.e. no value at all |
JavaScript non-primitive data types
The non-primitive data types are as follows:
| Data Type | Description |
|---|---|
| Object | represents instance through which we can access members |
| Array | represents group of similar values |
| RegExp | represents regular expression |
JavaScript Operators:
Arithmetic Operators
| Operator | Description |
|---|---|
| + | Addition |
| - | Subtraction |
| * | Multiplication |
| ** | Exponentiation |
| / | Division |
| % | Modulus (Division Remainder) |
| ++ | 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 |
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 |

0 Comments