Unit 2 Js wd-1
Unit 2 Js wd-1
It is used to create interactive and dynamic web pages, enabling features such as
real-time updates, form validation, animations, and much more.
JavaScript is a client-side language, meaning it runs in the user's web browser, but it
can also be used on the server-side with environments like Node.js.
● JavaScript is case-sensitive.
● Statements should end in a semicolon (;).
● Variables:
● Must be defined before being used. The variable name
can contain A – Z, a – z, underscore or digits and must
start with a letter or an underscore (“_”).
● Assume the type of the data that is put into the variable.
The data type does not have to be explicitly defined
● Are global variables when defined outside of a function,
and are available anywhere in the current script context.
Variables created within a function are local variables,
and can be used only within that function.
● Strings have to be enclosed in quotation marks, either a single or
double. For example: print(”Hello ” + ‘world ‘+
Country.name) produces the following: Hello world US.
● To increment a variable, such as a = a + 1, you can use a++.
You can decrement a variable in the same way, as in a--.
● To enter comments in the script, use "//"(single line comment)
to start a single line comment or the combination of "/*" and "*/"
to enclose a multi-line comment.
JavaScript Statements
JavaScript statements are made of: Values, Operators, Keywords,
Expressions, and Comments. JavaScript statements are executed in the
same order as they are written, line by line.
Semicolons
Example:
let a, b, c;
a = 2;
b = 3;
c = a + b;
The value of c is 5.
Code Blocks
Example:
function myFunction() {
console.log("Hello");
console.log("How are you?");
}
myFunction()
Output
Hello
How are you?
White Space
Example:
console.log(10*2);
console.log(10 * 2);
Output
20
20
JavaScript Functions
JavaScript functions are used to perform operations. We can call JavaScript
function many times to reuse the code.
function msg(){
</script>
Test it Now
<script>
function getcube(number){
alert(number*number*number);
</script>
<form>
</form>
Test it Now
JavaScript Variable
JavaScript variable
JavaScript Local variable
JavaScript Global variable
A JavaScript variable is simply a name of storage location. There are two types of
variables in JavaScript : local variable and global variable.
<script>
var x = 10;
var y = 20;
var z=x+y;
document.write(z);
</script>
Test it Now
<script>
function abc(){
</script>
<script>
function a(){
document.writeln(data);
function b(){
document.writeln(data);
</script>
JavaScript Objects
A javaScript object is an entity having state and behavior (properties and method).
For example: car, pen, bike, chair, glass, keyboard, monitor etc.
JavaScript is template based not class based. Here, we don't create class to get the
object. But, we direct create objects.
1. By object literal
2. By creating instance of Object directly (using new keyword)
3. By using an object constructor (using new keyword)
1. object={property1:value1,property2:value2.....propertyN:valueN}
1. <script>
2. emp={id:102,name:"Shyam Kumar",salary:40000}
3. document.write(emp.id+" "+emp.name+" "+emp.salary);
4. </script>
Test it Now
1. <script>
2. var emp=new Object();
3. emp.id=101;
4. emp.name="Ravi Malik";
5. emp.salary=50000;
6. document.write(emp.id+" "+emp.name+" "+emp.salary);
7. </script>
Test it Now
1. <script>
2. function emp(id,name,salary){
3. this.id=id;
4. this.name=name;
5. this.salary=salary;
6. }
7. e=new emp(103,"Vimal Jaiswal",30000);
8.
9. document.write(e.id+" "+e.name+" "+e.salary);
10. </script>
Test it Now
JavaScript If-else
The JavaScript if-else statement is used to execute the code whether condition is
true or false. There are three forms of if statement in JavaScript.
1. If Statement
2. If else statement
3. if else if statement
JavaScript If statement
It evaluates the content only if expression is true. The signature of JavaScript if
statement is given below.
if(expression){
//content to be evaluated
var a=20;
if(a>10){
</script>
Test it Now
if(expression){
else{
<script>
var a=20;
if(a%2==0){
else{
document.write("a is odd number");
</script>
Test it Now
a is even number
if(expression1){
else if(expression2){
else if(expression3){
else{
<script>
var a=20;
if(a==10){
else if(a==15){
else if(a==20){
else{
</script>
Test it Now
a is equal to 20
JavaScript Loops
The JavaScript loops are used to iterate the piece of code using for, while, do while
or for-in loops. It makes the code compact. It is mostly used in array.
1. for loop
2. while loop
3. do-while loop
code to be executed
<script>
document.write(i + "<br/>")
}
</script>
Test it Now
Output:
while (condition)
code to be executed
<script>
var i=11;
while (i<=15)
{
document.write(i + "<br/>");
i++;
</script>
Test it Now
Output:
11
12
13
14
15
do{
code to be executed
}while (condition);
<script>
var i=21;
do{
document.write(i + "<br/>");
i++;
}while (i<=25);
</script>
Test it Now
Output:
21
22
23
24
25
JavaScript Comments
JavaScript comments are used to explain the code to make it more
the code.
Single Line Comments
(//),
Syntax:
Javascript
console.log("Hello Geeks!");
Output
Hello Geeks!
Multi-line Comments
A multiline comment in JavaScript is a way to include comments that span
multiple lines in the source code.
Syntax:
/*
This is a multiline comment
It can span multiple lines
*/
Javascript
console.log("Multiline comment in
javascript");
JavaScript Operators
JavaScript operators are symbols that are used to perform operations on operands.
For example:
1. var sum=10+20;
1. Arithmetic Operators
2. Comparison (Relational) Operators
3. Bitwise Operators
4. Logical Operators
5. Assignment Operators
6. Special Operators
+ Addition 10+20 = 30
- Subtraction 20-10 = 10
* Multiplication 10*20 = 200
/ Division 20/10 = 2
% Modulus 20%10 = 0
(Remainder)
== Is equal to 10==20 =
false
=== Identical (equal and of same type) 10==20 =
false
false
false
JavaScript Bitwise Operators
The bitwise operators perform bitwise operations on operands. The bitwise
operators are as follows:
false
false
= Assign 10+10 = 20
assign
assign
assign
Operator Description
(?:) Conditional Operator returns value based on the condition. It is like if-else.
statement.
delete Delete Operator deletes a property from the object.
JavaScript is a dynamic type language, means you don't need to specify type of the
variable because it is dynamically used by JavaScript engine. You need to use var
here to specify the data type. It can hold any type of values such as numbers, strings
etc. For example:
1. var a=40;//holding number
2. var b="Rahul";//holding string
Data Description
Type
JavaScript String
The JavaScript string is an object that represents a sequence of characters.
1. By string literal
2. By string object (using new keyword)
1) By string literal
The string literal is created using double quotes. The syntax of creating string using
string literal is given below:
<script>
var str="This is string literal";
document.write(str);
</script>
Test it Now
Output:
The syntax of creating string object using new keyword is given below:
<script>
document.write(stringname);
</script>
Test it Now
Output:
The JavaScript String charAt() method returns the character at the given index.
1. <script>
2. var str="javascript";
3. document.write(str.charAt(2));
4. </script>
Test it Now
Output:
1. <script>
2. var s1="javascript ";
3. var s2="concat example";
4. var s3=s1.concat(s2);
5. document.write(s3);
6. </script>
Test it Now
Output:
The JavaScript String indexOf(str) method returns the index position of the given
string.
1. <script>
2. var s1="javascript from javatpoint indexof";
3. var n=s1.indexOf("f");
4. document.write(n);
5. </script>
Test it Now
Output:
11
The JavaScript String toLowerCase() method returns the given string in lowercase
letters.
1. <script>
2. var s1="JavaScript toLowerCase Example";
3. var s2=s1.toLowerCase();
4. document.write(s2);
5. </script>
Test it Now
Output:
javascript tolowercase example
The JavaScript String toUpperCase() method returns the given string in uppercase
letters.
<script>
var s2=s1.toUpperCase();
document.write(s2);
</script>
Test it Now
Output:
Numeric arrays, specifically, are arrays that primarily contain numbers. Here's a
comprehensive guide to working with numeric arrays in JavaScript:
javascript
Copy code
javascript
Copy code
javascript
Copy code
Copy code
console.log(numbers[0]); // Output: 1
console.log(numbers[4]); // Output: 5