java scrpit notes 1
java scrpit notes 1
What is JavaScript?
• JavaScript is a popular client side scripting language that is supported by all web
browsers. It is used on the client side of web development.
• JavaScript is a dynamic and lightweight programming language.
• It is an interpreted language with object-oriented features.
• JavaScript is known for its powerful capabilities in client-side scripting.
• It is mainly used to enhance user interaction on webpages, making them more lively
and interactive.
• In addition to web development, JavaScript is widely used in game development and
mobile application development.
History of JavaScript
• JavaScript was created by Brendan Eich in 1995.
• Initially, it was called LiveScript, but later its name was changed to JavaScript.
Features of JavaScript
• JavaScript is a scripting language that is based on objects.
• It gives users more control over their web browsers.
• JavaScript can handle dates and time.
• It can detect the user's browser and operating system.
• JavaScript is lightweight.
• JavaScript is a separate language from Java, despite the similar name.
• It is an interpreter-based scripting language.
• JavaScript is case sensitive, meaning that uppercase and lowercase letters are
distinct.
• JavaScript is object-based, providing predefined objects.
• Every statement in JavaScript must end with a semicolon (;).
• The syntax of most JavaScript control statements is similar to that of control
statements in the C programming language
Limitations of JavaScript
1. File access limitations: Client-side JavaScript does not have the ability to read or
write files due to security reasons.
2. Networking limitations: JavaScript lacks support for networking applications.
3. Lack of multi-threading and multiprocessor capabilities: JavaScript does not
have built-in features for multi-threading or utilizing multiple processors.
4. Performance limitations: JavaScript can be slower compared to other languages,
which may affect performance, especially in complex applications or heavy
computations.
5. Browser compatibility issues: JavaScript may work differently across different web
browsers, requiring developers to test and ensure consistent behavior across
platforms.
6. Limited support for low-level operations: JavaScript doesn't have built-in features
for performing low-level operations like manipulating binary data or directly accessing
computer memory, which some other languages like C or C++ support.
What is Script?
- A Scripting Language is a type of computer programming language used to provide
instructions called scripts, to software application,etc.
Types of Script
1] Client Side Scripting Language
• It executes on client side on their web browser.
• Client-side scripts have the advantage of reducing server demand, allowing web
pages to load quicker.
• Ex. HTML, CSS, JavaScript.
2] Server Side Scripting Language
Enhances user experience and interactivity Handles server-side processing and data
on the website. manipulation.
Can be used for immediate feedback and Allows for secure handling of sensitive
validation of user inputs. information on the server.
Requires a compatible web browser to Executes on the server and sends the
execute the code. results to the user's browser.
It does not provide security for data. It provides more security for data.
Examples include JavaScript, HTML, and Examples include PHP, Python, and Ruby.
CSS.
JavaScript Tag:
• JavaScript is implemented using the <script>...</script> tags in an HTML webpage.
• The <script> tags can be placed anywhere within the webpage, but it is commonly
recommended to put them within the <head> tags.
• The <script> tag notifies the browser to interpret the content between the tags as
JavaScript code
• The basic syntax for including JavaScript code is:
<script language="javascript" type="text/javascript">
// JavaScript code
</script>
<html>
<body>
<script language="javascript" type="text/javascript">
document.write("Hello World!");
</script>
</body>
</html>
Terms of JavaScript
1] Method
• A property is a value associated with an object. Every object has its own set of
properties.
• Ex. Window has properties like width, height, background color.
• A property is a key:value pair.
o Key(Property name) is always a string.
o Value(Property value) can be of any data type.
3] Event / Main Event
• JavaScript’s interaction with HTML is handled through events that occur when user
or browser manipulates a page.
• When the page load, it is called an event. When user clicks on button, that click is an
event too.
4] Object-Name
• Every object is associated with some property and method. For accessing the
properties and methods of an object we use dot operator.
• Eg document.write(“Hello”); //Accessing method write of document object.
function example(){
var y=10; //Local Variable
document.write("Global X="+x);
document.write("Local Y="+y);
}
example();
</script>
</body>
</html>
There are five types of primitive data types in JavaScript. They are as follows:
Types of Comments:
There are two types of comments in JavaScript: single-line comments and multi-line
comments.
1] Single-line comments:
- In JavaScript, use double slashes (//) to write a single-line comment.
- Single-line comments start with // and continue until the end of the line.
- You can use single-line comments on multiple lines by adding // at the beginning of
each line.
- Example
<html><body>
<script language="javascript" type="text/javascript">
// This is a JavaScript program that displays a message.
// This program was developed by MahindraTech Company.
document.write("Welcome to the world of JavaScript");
</script>
</body></html>
2] Multi-line comments:
- In JavaScript, multi-line comments start with /* and end with */.
- They can cover across multiple lines.
- Multi-line comments are useful for providing longer explanations.
- Example
<html><body>
<script language="javascript" type="text/javascript">
// This is a JavaScript program that displays a message.
// This program was developed by MahindraTech Company.
document.write("Welcome to the world of JavaScript");
</script>
</body></html>
Operators:
- Operator is a symbol which indicates operation to be performed.
- Operands are the variable on which operation to be performed.
- The systematic arrangement of operators and operands is known as Expression.
Example
<html> <body>
var a = 5;
var b = 3;
</script>
</body></html>
Example
<html>
<body>
<script language="javascript" type="text/javascript">
var a = 5;
var b = 3;
document.write("<br>Equal to (==): " + (a == b)); // false
document.write("<br>Not equal to (!=): " + (a != b)); // true
document.write("<br>Less than (<): " + (a < b)); // false
document.write("<br>Greater than (>): " + (a > b)); // true
document.write("<br>Less than or equal to (<=): " + (a <= b)); // false
document.write("<br>Greater than or equal to (>=): " + (a >= b)); // true
document.write("<br>Strict equal to (===): " + (a === b)); // false
document.write("<br>Strict not equal to (!==): " + (a !== b)); // true
</script>
</body>
</html>
Example
<html>
<body>
var a = 5;
var b = 3;
</script>
</body>
</html>
Operator Meaning
&& It returns true if its both sides are true otherwise false
|| It returns false if its both sides are false otherwise true
! Reverse the Result, returns false if the result is true.
- Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var a = true;
var b = false;
document.write("<br>Logical AND (&&): " + (a && b)); // false
document.write("<br>Logical OR (||): " + (a || b)); // true
document.write("<br>Logical NOT (!): " + !a); // false
</script>
</body>
</html>
- Assignment operator is used to assign right side expression result or value to the
left side variable.
- Assignment operators are used in JavaScript to assign values to variables.
- The following operators are known as JavaScript assignment operators.
Example:
<html>
<body>
var a = 5;
var b = 3;
</script>
</body>
</html
Example:
<html>
<body>
var a = 100;
var b = 20;
var result;
result = (a > b) ? a : b;
document.write("Greatest Number="+result); //100
</script>
</body>
</html>
Example:
var a =10;
document.write(a++); //11
document.write(a--); //9
1] Primary Expression:
- These are the simplest expressions and include basic values like numbers, strings,
and boolean values.
- For example, 5, "Hello", and true are primary expressions.
- Number Literal – 3.2, String literal – “Welcome”, Regular Exp Literal - / pattern /
2] Array and Object Initializers Expressions:
- These are the expressions whose values are created by object or array. These are
also known as “Object Literals” or “Array Literals”.
- These expressions allow us to create arrays and objects.
- We can define a list of values enclosed in square brackets [ ] for arrays and key-
value pairs enclosed in curly braces { } for objects.
- For example, [1, 2, 3] is an array initializer expression, and { name: "Laxmikant", age:
18 } is an object initializer expression.
3] Function Defination Expressions:
1] if Statement
- It is a simple decision making statement.
- It is used to test a single condition.
- If the condition is true, then it will execute the body of if statement
- If the condition is false, then it will skip the execution of body of if statement.
Syntax:
if (condition) {
// code to execute if condition is true
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var age = 20;
if (age >= 18) {
document.write("You are eligible for voting"); // Outputs if the condition is true
}
</script>
</body>
</html>
if (condition2) {
// code block executed if condition2 is true
} else {
// code block executed if condition2 is false
}
} else {
// code block executed if condition1 is false
}
Example:
<html><body>
<script language="javascript" type="text/javascript">
var no=5;
if(no!=0){
if(no>0){
document.write("Positive Number");
}
else{
document.write("Negative Number")
}
}
else{
document.write("Zero Number")
}
</script></body></html> Output: Positive Number
switch (no) {
case 1:
document.write("1");
break;
case 2:
document.write("2");
break;
case 3: document.write("3");
break;
default:
document.write("Invalid Input");
} </script> </body></html>
1] For Loop
- For Loop is a entry controlled loop.
- It executes the block of code specified number of times.
- If we know the number of iterations to be executed, then we can use the for loop.
- Program controller first goes to initialization section, then checks the condition, if
condition is true, then it will execute the body of loop, then it goes to
increment/decrement section, then again checks the condition. And this process is
repeated till condition becomes false.
Syntax:
for (initialization; condition; increment/decrement) {
//body of for loop
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
for(var i=0; i<=5; i++){
document.write(i+" ");
}
</script>
</body>
</html>
Output : 0 1 2 3 4 5
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var i=0;
while(i<=5){
document.write(i+" ");
i++;
}
</script>
</body>
</html>
Output : 0 1 2 3 4 5
Output : 0 1 2 3 4 5
1] Break Statement
- The break statement is used to transfer the control to the end of the loop.
- When break statement is applied then loop gets terminate and control goes to the
next line pointing after the loop body.
- It is generally used with decision making statement such as if-else.
- It is used to terminate a loop.
Syntax:
If(condition){
break;
}
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
for(var i=0; i<=5; i++){
if(i==3){
break;
}
document.write(i+" ");
}
</script>
</body>
</html>
Output : 0 1 2
Output : 0 1 2 4 5
Here, we have created a person object with the properties as name, age, gender.
Querying Properties:
To retrieve the value of a property from an object, you can use dot notation or square
bracket notation.
1] Dot Notation: Use the dot operator after the object name to access the property value
directly.
Example: var person = { name: 'Laxmikant', age: 18 };
document.write(person.name); // Output: Laxmikant
2] Square Bracket Notation: Enclose the property name in square brackets after the
object name to access the property value.
Example: var person = { name: 'Laxmikant', age: 18 };
document.write(person['age']); // Output: 18
Setting Properties:
To assign a new value to a property or create a new property in an object, you can
use assignment with dot or square bracket notation.
1] Dot Notation: Use the dot operator to assign a new value to an existing property or
create a new property in the object.
Example: var person = { name: 'Laxmikant', age: 18 };
person.age = 30; // Modifying existing property
2] Square Bracket Notation: Use square brackets with the property name as a string to
assign a new value or create a new property.
Example: var person = { name: 'Laxmikant', age: 18 };
person['gender'] = 'Male'; // Adding new property
1] Getter Method:
In JavaScript, getter methods are used to access the properties of an object.
2] Setter Method:
Setter methods are used to change the value of object.
Example:
<html> <body>
<script language="javascript" type="text/javascript">
var student = {
name: "Laxmikant",
age: 18,
get getName() {
return this.name;
},
set setName(nm) {
this.name = nm;
}
};
Example:
<html>
<body>
<script language="javascript" type="text/javascript">
var student = {
name: "Laxmikant",
id: 352,
rollno: 5
};
document.write(delete student.id);
</script>
</body>
</html>