Js Part 1 PDF
Js Part 1 PDF
<html>
<body>
<script language = "javascript" type = "text/javascript">
console.log("Hello World!")
</script>
</body>
</html>
Applications of Javascript Programming
As mentioned before, Javascript is one of the most widely used programming languages (Front-end as well as
Back-end). It has it's presence in almost every area of software development. I'm going to list few of them here:
•Client side validation - This is really important to verify any user input before submitting it to the server and
•Manipulating HTML Pages - Javascript helps in manipulating HTML page on the fly. This helps in adding and
deleting any HTML tag very easily using javascript and modify your HTML to change its look and feel based on
•User Notifications - You can use Javascript to raise dynamic pop-ups on the webpages to give different types of
•Less server interaction − You can validate user input before sending the page off to the server. This saves
•Immediate feedback to the visitors − They don't have to wait for a page reload to see if they have
•Increased interactivity − You can create interfaces that react when the user hovers over them with a
•Richer interfaces − You can use JavaScript to include such items as drag-and-drop components and
security reason.
•JavaScript cannot be used for networking applications because there is no such support
available.
<script>... </script>
You can place the <script> tags, containing your JavaScript, anywhere within your web page, but it is
normally recommended that you should keep it within the <head> tags.
Attributes : •Language − This attribute specifies what scripting language you are using. Typically, its
value will be javascript. Although recent versions of HTML (and XHTML, its successor) have
phased out the use of this attribute.
•Type − This attribute is what is now recommended to indicate the scripting language in use
and its value should be set to "text/javascript".
Comments in JavaScript
JavaScript supports both C-style and C++-style comments, Thus −
•Any text between a // and the end of a line is treated as a comment and is ignored by JavaScript.
•Any text between the characters /* and */ is treated as a comment. This may span multiple lines.
•JavaScript also recognizes the HTML comment opening sequence <!--. JavaScript treats this as a single-line
•The HTML comment closing sequence --> is not recognized by JavaScript so it should be written as //-->.
Case Sensitivity
JavaScript is a case-sensitive language. This means that the language keywords, variables,
function names, and any other identifiers must always be typed with a consistent capitalization of
letters.
So the identifiers Time and TIME will convey different meanings in JavaScript.
JavaScript in <head>...</head> section
<html>
<head>
<script type = "text/javascript">
<!-- function sayHello() {
alert("Hello World")
} //-->
</script>
</head>
<body>
<input type = "button" onclick = "sayHello()" value = "Say Hello" />
</body>
</html>
JavaScript in <body>...</body> section
<html>
<head>
</head>
<body>
<script type = "text/javascript">
<!-- console.log("Hello World") //-->
</script>
<p>This is web page body </p>
</body>
</html>
Developer console
To see errors and get a lot of other useful information about scripts, “developer tools” have been embedded in
browsers.
Google Chrome : Press F12 The developer tools will open on the Console tab by default.
• Developer tools allow us to see errors, run commands, examine variables, and much more.
• They can be opened with F12 for most browsers on Windows. Chrome for Mac needs Cmd+Opt+J,
Safari: Cmd+Opt+C (need to enable first).
JavaScript in External File
The script tag provides a mechanism to allow you to store JavaScript in an external file and then include it into
your HTML files.
First.html
<html>
<head>
<script type = "text/javascript" src = "filename.js" >
</script> </head>
<body> ....... </body>
</html>
Filename.js
alert("Hello World")
JavaScript Datatypes
JavaScript also defines two trivial data types, null and undefined, each of which defines only a single value.
In addition to these primitive data types, JavaScript supports a composite data type known as object.
JavaScript Variables
Variables are declared with the var keyword as follows.
var money;
var money; var name;
var name;
•Global Variables − A global variable has global scope which means it can be defined anywhere
in your JavaScript code.
•Local Variables − A local variable will be visible only within a function where it is defined.
Function parameters are always local to that function.
<html>
<body>
<script type = "text/javascript">
var myVar = "global"; // Declare a global variable
function checkscope( ) {
var myVar = "local"; // Declare a local variable
console.log(myVar);
}
checkscope();
</script>
</body>
</html>
JavaScript Variable Names
•should not use any of the JavaScript reserved keywords
•should not start with a numeral (0-9). They must begin with a letter or an underscore character.
For example, 123test is an invalid variable name but _123test is a valid one.
•Arithmetic Operators
•Comparison Operators
•Logical (or Relational) Operators
•Assignment Operators
•Conditional (or ternary) Operators
Arithmetic operators let x = 5;
let y = 3;
console.log('x + y = ', x + y);
Addition (+) console.log('x - y = ', x - y);
Subtraction (-) console.log('x * y = ', x * y);
Multiplication (*) console.log('x / y = ', x / y);
Division(/) console.log('x % y = ', x % y);
Modulus(%)
console.log('++x = ', ++x);
console.log('x++ = ', x++);
Increment (++)
console.log('x = ', x);
console.log('--x = ', --x);
Decrement(--)
Exponent (**)
console.log('x-- = ', x--);
console.log('x ** y =', x ** y);
JavaScript Comparison Operators
The JavaScript comparison operator compares the two operands.
= Assign 10+10 = 20
+= Add and assign var a=10; a+=20; Now a = 30
String Conversion
String conversion happens when we need the string form of a value.
if(expression)
{
//content to be evaluated ;
}
<script>
var a=20;
if(a>10)
{
console.log("value of a is greater than 10");
}
</script>
If...else Statement
It evaluates the content whether condition is true of false. The syntax of JavaScript if-else
statement is given below.
if(expression){
//content to be evaluated if condition is true
}
else{
//content to be evaluated if condition is false
}
<script>
var a=20;
if(a%2==0){
console.log("a is even number");
}
else{
console.log("a is odd number");
}
</script>
Switch
The JavaScript switch statement is used to execute one code from multiple expressions. It is
just like else if statement that we have learned in previous page. But it is convenient
than if..else..if because it can be used with numbers, characters etc.
The signature of JavaScript switch statement is given below.
switch(expression)
{
case value1:
code to be executed;
break;
case value2:
code to be executed;
break;
......
default:
code to be executed if above values are not match
ed;
}
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while loops.
It makes the code compact. It is mostly used in array.
1.for loop
2.while loop
3.do-while loop
let sum = 0;
Let i=1
while (i<10) {
let value =5;
if (i==value) break;
sum += i;
i++;
} alert( 'Sum: ' + sum );
Continue to the next iteration
The continue directive is a “lighter version” of break.
It doesn’t stop the whole loop. Instead, it stops the current iteration and forces the loop to start
a new one (if the condition allows).
We can use it if we’re done with the current iteration and would like to move on to the next one.