You are on page 1of 34

VJ TECH ACADEMY – JAVASCRIPT NOTES

Client Side Scripting (JavaScript)

Author:
Prof. Vishal Jadhav
[BE in Computer Engineering and Having 9 years of IT industry experience and 10 years of
teaching experience]

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 1
VJ TECH ACADEMY – JAVASCRIPT NOTES

UNIT 2 – Array, Functions and String

2.1 Array – declaring an Array, Initializing an Array, defining an Array


elements, Looping an Array, Adding an Array element, Sorting an array
element, Combining an Array elements into a String, changing elements of
an Array, Objects as associative Arrays.
2.2 Function – defining a function, writing a function, adding an arguments,
scope of variable and arguments.
2.3 Calling a Function – calling a function with or without arguments, calling
function from HTML, function calling another function, Returning a value
from a function.
2.4 String – manipulate a String, joining a string, retrieving a character from
given position, retrieving a position of character in a string, dividing a text,
copying a sub string, converting string to number and number to string,
changing the case of string, finding a Unicode of a character- charCodeAt(),
fromCharCode().

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 2
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript Array ( Overview )

- JavaScript Arrays represent the collection of elements (or items) stored under a single
variable.
- This simply means that when we want to store multiple items in a single variable, we can use
Arrays in JavaScript.
- Arrays in JavaScript can store the items belonging to any data type.
- In addition to this, there are many methods of Arrays in JavaScript that make it easy to work
with JavaScript Arrays.
- Let us dive deep into the world of Arrays in JavaScript.

 Introduction to JavaScript Arrays

- JavaScript Arrays are the objects that can store multiple items(or values) irrespective of their
data types under a single variable.
- In many programming languages, we can store multiple items in arrays, but they must be of
the same data type like we can not store a number value inside an array having string values.
- But in JavaScript, we have the freedom of storing any data type value in an array be it number,
string, null, undefined, float, or even an array can be stored inside an array.
- Every element in the array has an index associated with it. It means that we can access any
element directly using its index.

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 3
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Declaring JavaScript Arrays

- Now let us see how we can declare JavaScript Arrays. There are two ways of declaring a
JavaScript Array.

 Using the JavaScript ‘new’ Keyword (array constructor)


- As mentioned above, the arrays are objects in JavaScript; hence they can be created using
the new keyword also.
- Let us see an example :

<html>
<body>
<script language="javascript" type="text/javascript">
// Array declaration without initialization
var myArray = new Array();
myArray[0] = "Vishal";
myArray[1] = "Jadhav";
myArray[2] = "Ghanasham";
myArray[3] = "Irashetti";
document.write(myArray + "<br>");// print array elements

// Array declaration with initialization


var myArray2 = new Array("Vishal","Jadhav","Ghanasham","Irashetti");
document.write(myArray2 + "<br>");// display array elements

</script>
</body>
</html>

Output

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 4
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Using the JavaScript Array Literal


- The Array literal way allows us to add the elements separated by a comma and enclosed
inside square brackets “[ ]”.
- This is the easy as well as the preferred way of declaring JavaScript Arrays.
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var arr1 = ["Hello", "VJTECH", "Academy"];
document.write(arr1);
</script>
</body>
</html>

 Common Operations of JavaScript Array


- JavaScript provides several operations to work with arrays. Let us discuss them one by one.

 Creating an Array :
- As mentioned above, arrays can be created in two ways but we use for our syllabus
"constructor " way . So let us create an array using the [ new Array() ] constructor:

var myArray = new Array("VJTech", "Academy", "Batch", 2023);


console.log(myArray);
// output:-> ["VJTech", "Academy", "Batch", 2023,]

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 5
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Access an Array item using the index position :

- All of the array items are indexed, which means that every element of the array is assigned a
particular index based on its position from the very first element of the array, and the first
element has the index value of 0(zero).
- The main purpose of indexing is the ease of access.
- In simple words, we can easily access the array elements using their indexes to perform various
operations on those elements.
- Syntax: arrayName[index]

var myArray = new Array ("VJTech", "Academy", "Batch", 2023);


console.log(myArray[0]);
// output:-> VJTech

 Changing An Array Elements :

- To change the value of an array element first, we need to access it using its index and then
place an equal operator with the new desired value.

- Syntax: arrayName[index] = newValue

// changing array elements


var myArray = new Array("VJTech", "Academy", "Batch", 2023);
myArray[2]="JavaScript Batch";
console.log(myArray);
// output:-> ["VJTech", "Academy", "JavaScript Batch", 2023,]

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 6
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Loop Over an Array:

- If we want to access all the elements of the JavaScript Array, then we can use for (or for-each)
loop.
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
// loop over an array
var myArray = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
for (var i = 0; i < myArray.length; i++) {
document.write(myArray[i] + "<br>");
}
</script>
</body>
</html>

- Output :

 Add an Item to the Beginning of an Array :

- If we want to add an element (or elements) at the beginning of JavaScript Array, we can use
an array method called unshift().
- The unshift() method as shifts the previously stored elements to the right and inserts the new
specified element(s) at the first index position of the array.

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 7
VJ TECH ACADEMY – JAVASCRIPT NOTES

- For example:

<html>
<body>
<script language="javascript" type="text/javascript">
// add elements to beginning of an array
var myArray = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
myArray.unshift("Hello");
console.log(myArray);
// output:-> ["Hello", "Vishal", "Jadhav", "VJTECH", "ACADEMY",]
</script>
</body>
</html>

 Add an Item to the End of an Array ( 2 ways ):

- If we want to add an element (or more) to the end of a JavaScript Array, we can use an array
method called push().
- The push() method as the name suggests, pushes the specified element(or elements) to the end
of an array.

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 8
VJ TECH ACADEMY – JAVASCRIPT NOTES

- Example using push() :


<script language="javascript" type="text/javascript">
// add elements to end of an array
var myArray = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
myArray.push("Coding");
console.log(myArray);
// output:-> ["Vishal", "Jadhav", "VJTECH", "ACADEMY", "Coding",]
</script>

- Example using length property of array :

<script language="javascript" type="text/javascript">


// add element using length property of an array
var myArray = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
myArray[myArray.length]="Hello";
console.log(myArray);
// output:-> ["Vishal", "Jadhav", "VJTECH", "ACADEMY", "Hello",]
</script>

 Remove an Item from the Beginning of an Array :

- If we want to remove an element from the beginning of JavaScript Array, we can use an array
method called shift().
- The shift() method as the name suggests, shifts the array elements to the left by removing an
element from the beginning of the array.

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 9
VJ TECH ACADEMY – JAVASCRIPT NOTES

- For example :

<script language="javascript" type="text/javascript">


// remove elements from beginning of an array
var myArray = new Array("Welcome", "Vishal", "Jadhav", "VJTECH",
"ACADEMY");
myArray.shift();
console.log(myArray);
// output:-> ["Vishal", "Jadhav", "VJTECH", "ACADEMY",]
</script>

 Remove an Item from the End of an Array:

- If we want remove an element from the end of JavaScript Array, we can use an array method
called as pop().
- The pop() method as the name suggests pops an element from the end of an array.

- For example :

<script language="javascript" type="text/javascript">


// remove elements from end of an array
var myArray = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
myArray.pop();
console.log(myArray);
// output:-> ["Vishal", "Jadhav", "VJTECH",]
</script>

In this code “ACADEMY” is popped from ‘myArray’

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 10
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Combine an Array Elements :


- Sometime there is need to combine all elements of array into a single string.
- For Example : var numbers=new Array("one","two","three");
- By combining the array elements means to display contents of all array elements as string.
- For example: "one,two,three"
- We can combine array elements by using two different ways

1. Using concat() method :


- In this method we can combine two or more array elements together but it is separated by
comma.
- Syntax:
ArrayObjName.concat();
ArrayObjName.concat(ArrayObj2,ArrayObj3...);
2. Using join() method :
- ‘join()’ method returns a string by concatenating all the elements of an array separated by
commas or a specified separator string.
- If the array has only one item, then that item will be returned without using the separator.
- If the array is empty, then an empty string will be returned.
- Syntax:
ArrayObjName.join(‘separator’)
3. Using slice() method :
- ‘slice()’ method returns a new array by extracting the elements of an existing array.
- The extracted elements will be from the start index to the end index. The end index is
optional.
- Syntax :
ArrayObjName.slice(startIndex, endIndex);

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 11
VJ TECH ACADEMY – JAVASCRIPT NOTES

- Example For Combine Array Elements

<html>
<body>
<script language="javascript" type="text/javascript">
// 1. using join() method
var myArray1 = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
var string = myArray1.join('-');
document.write(string);
document.write("<br>");

// 2. using concat() method


var myArray2 = new Array("Online", "Classes", "VJTECH", "ACADEMY");
var myArray3 = new Array("Welcome", "Students");
var string = myArray2.concat(myArray3);
document.write(string);
document.write("<br>");

// 3. using slice() method


var myArray4 = new Array("Vishal", "Jadhav", "VJTECH", "ACADEMY");
var string = myArray4.slice(2, 4);
document.write(string);
document.write("<br>");
</script>
</body>
</html>

- Output :

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 12
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Sorting an Array Elements :


- Sorting an array in JavaScript can be done using the ‘sort()’ method.
- By default, the ‘sort()’ method sorts elements as strings.
- To sort numbers correctly, you need to provide a custom comparison function.

Sorting an Array of Numbers:

<html>
<body>
<script language="javascript" type="text/javascript">
var numbers = [5, 2, 8, 1, 9];
numbers.sort(function (a, b) {
return a - b; // For ascending order
// return b - a; // For descending order
});
document.write(numbers);
document.write("<br>");
// output:-> [1, 2, 5, 8, 9,]
</script>
</body>
</html>

- In this example, the sort() method is called on the numbers array.


- The comparison function takes two parameters, ‘a’ and ‘b’, representing elements of the array.
Subtracting ‘b’ from ‘a’ sorts the numbers in ascending order.
- To sort in descending order, subtract ‘a’ from ‘b’.

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 13
VJ TECH ACADEMY – JAVASCRIPT NOTES

Sorting an Array of Numbers Using Loop:


<script language="javascript" type="text/javascript">
// sort numbers array logically (ascending order) using loop
var numArray = [10, 5, 40, 25, 100, 1];
var temp;
for (var i = 0; i < numArray.length; i++) {
for (var j = i + 1; j < numArray.length; j++) {
if (numArray[i] > numArray[j]) {
temp = numArray[i];
numArray[i] = numArray[j];
numArray[j] = temp;
}
}
}
document.write(numArray);
document.write("<br>");
// output:-> [1, 5, 10, 25, 40, 100,]
</script>

<script language="javascript" type="text/javascript">


// sort numbers array logically ( descending order ) using loop
var numArray = new Array(10, 5, 40, 25, 100, 1);
var temp;
for (var i = 0; i < numArray.length; i++) {
for (var j = i + 1; j < numArray.length; j++) {
if (numArray[i] < numArray[j]) {
temp = numArray[i];
numArray[i] = numArray[j];
numArray[j] = temp;
}
}
}
document.write(numArray);
document.write("<br>");
JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 14
VJ TECH ACADEMY – JAVASCRIPT NOTES
</script>

Sorting an Array of Strings:

<html>
<body>
<script language="javascript" type="text/javascript">
var fruits = ['banana', 'apple', 'pear', 'grape'];
fruits.sort(); // Sorted alphabetically by default
document.write(fruits);
document.write("<br>");
// output:-> ["apple", "banana", "grape", "pear",]
</script>
</body>
</html>

- In this example, the sort() method is called on the fruits array. By default, strings are sorted
alphabetically.

 Associative Array In JavaScript


- In JavaScript, objects can be used as associative arrays, also known as dictionaries or maps.
- This approach allows you to use strings (or other data types) as keys to access values,
effectively creating a mapping between keys and their corresponding values.
- Here's how you can use objects as associative arrays:

 Creating an Associative Array :


- You can create an object and use its properties as key-value pairs in your associative array.

var person = {
firstName: "Visham",
JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 15
VJ TECH ACADEMY – JAVASCRIPT NOTES

lastName: "Jadhav",
age: 30
};

 Accessing Values :
- You can access values in the associative array using either dot notation or square bracket
notation.

document.write(person.firstName);
// Vishal (using dot notation)
document.write(person['lastName']);
// Jadhav (using square bracket notation)

 Looping through Associative Array :


- You can loop through the key-value pairs in an associative array using a ‘for...in’ loop.

for (var key in person) {


document.write(key + ": " + person[key] + "<br>");
}

Code :
<html>
<body>
<script language="javascript" type="text/javascript">
var person = {
firstName: "Vishal",
lastName: "Jadhav",
age: 25,
}
document.write(person.firstName);
document.write(person['lastName']);
for (var key in person) {
document.write(key + ": " + person[key] + "<br>");
}
</script>
</body>
</html>
JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 16
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript Functions :

- JavaScript functions are a set of statements that perform a task or calculate a value. Values
can be passed to a function as parameters, and the function will return a value.
- A function in JavaScript is a reusable block of code that performs a specific task or set of
tasks.
- Functions play a crucial role in structuring code, promoting reusability, and making complex
tasks manageable.
- In JavaScript two types of functions is there :
1. Built-In-Functions
2. User Defined Functions

 Built In Functions :
- JavaScript provides a variety of built-in functions that are readily available for performing
common tasks.
- These functions are part of the JavaScript language itself and don't require additional setup or
installation.
1. document.write() : To display/print information to the browser window.
2. console.log() : Used for logging information to the console (browser's developer
console or terminal).
3. alert() : Displays a popup dialog box with a message to the user.
4. prompt() : Displays a dialog box prompting the user for input.
5. confirm() : Displays a dialog box asking the user to confirm or cancel an action.
6. parseInt() & parseFloat() : Converts strings to integers and floating-point numbers
respectively.
- These are some functions listed here… we can explore much more in upcoming chapters.

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 17
VJ TECH ACADEMY – JAVASCRIPT NOTES

 User Defined Functions :


- A user-defined function is a custom code block that encapsulates a set of statements to perform
a specific task.
- They allow you to modularize your code by breaking it into smaller, reusable pieces.
- User-defined functions enhance code readability, reusability, and maintainability.
- Syntax :

function functionName(parameters) {
// Code block to perform a task
return result; // Optional: return a value
}

 ‘function’: The keyword that starts the function declaration.


 ‘functionName’: The name you give to the function. Choose a meaningful name to
describe its purpose.
 ‘parameters’: Input values that the function can accept. These are placeholders for actual
values passed when the function is called.
 ‘return’: An optional keyword used to send a value back as the result of the function's
execution.

 Calling Functions Without Arguments:

- When you call a function without passing any arguments, the function executes its code with
the absence of input values.
- This can be useful for functions that perform tasks or calculations that don't require external
data.
- Syntax :
function functionName() {
// Function code
}
JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 18
VJ TECH ACADEMY – JAVASCRIPT NOTES
functionName(); // Calling the function without arguments

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 19
VJ TECH ACADEMY – JAVASCRIPT NOTES

- Example: Function Without Arguments:


<html>
<body>
<script language="javascript" type="text/javascript">
// user defined functions
function sayHello() {
document.write("Hello, welcome!");
}
sayHello(); // Output: Hello, welcome!
</script>
</body>
</html>

 Calling Functions Without Arguments:


- Functions can also accept arguments, which are values provided when the function is called.
- These arguments act as inputs that the function can work with.
- Syntax for Defining and Calling a Function with Arguments:
function functionName(parameter1, parameter2) {
// Function code using parameters
}
functionName(argument1, argument2); // Calling the function with arguments

- Example: Function With Arguments:

<html>
<body>
<script language="javascript" type="text/javascript">
function add(a, b) {
return a + b;
}
var result = add(5, 3); // Calling the function with arguments
document.write(result); // Output: 8
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 20
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Calling Functions From HTML :

- Calling JavaScript functions from HTML is a common practice to create interactive and
dynamic web pages.
- You can achieve this by embedding your JavaScript code within <script> tags in your HTML
file.
- You can place your JavaScript code directly within the HTML file using the ‘<script>’ tag.
This is suitable for small snippets of code.
- Example :
<html>
<head>
<title>Calling JavaScript Functions</title>
</head>
<body>
<button onclick="sayHello()">Click me</button>
<script>
function sayHello() {
alert("Hello, World!");
}
</script>
</body>
</html>

[ Note : You also use external JavaScript file. This file can contain all JavaScript code and we
save this file using ‘.js’ and link in html file using the ‘<script>’ tag. ]

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 21
VJ TECH ACADEMY – JAVASCRIPT NOTES

 String In JavaScript :

- The string is a primitive data type in JavaScript.


- Strings in JavaScript are used to store textual forms of data such as words, sentences,
paragraphs, etc.
- Strings in JavaScript should be enclosed within single ticks or double ticks.
- Strings in JavaScript follow 0-based indexing.

 Ways to Create a String in JavaScript


- Strings in JavaScript can be created by two methods. Strings in JavaScript can be created:
- as primitives from string literals
- as object using ‘string()’ constructor

1. Creating strings as primitives:


- Strings in JavaScript can be created by enclosing the string text within single ticks (' '), or
double ticks (" "), or backticks (\) This string is then assigned to a constant or variable.
- Syntax :

var str1 = "String within double ticks";


var str2 = 'String within single ticks';
var str3 = `String within backticks`;

2. Creating strings as an object :


- Strings in JavaScript can be created by calling the String() constructor with the string as
the parameter.
- The String() constructor is called using the new keyword and the value is assigned to a
variable or constructor.
- Syntax :

var str4 = new String("This string object is created by calling the


constructor");

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 22
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Ways to Access Individual Characters in a String


- Strings in JavaScript follow 0-based indexing, i.e., the first character of the string is considered
as the 0th character, the second character of the string is considered as the 1st character, and
so on.
- For e.g., if we have string "abc", the a is the 0th character, b is the 1st character, and c is the
2nd character.
- Thus in order to access an individual character of the string, we need to call the string along
with the character index.
- There are two ways to access the string character with an index:
- By using charAt() method:
 We can access the individual character of strings in JavaScript by calling the charAt()
method along with string using the dot (.) operator and passing the index as the parameter
to the charAt() method.
 Syntax :
str.charAt(index);

 Example :
<html>
<body>
<script language="javascript" type="text/javascript">
var str = "Welcome to VJTech Academy";
document.write(str.charAt(0));
// output :-> W
document.write(str.charAt(11));
// output :-> V
</script>
</body>
</html>

Another one more way is using Bracket ‘[ ]’ Notation : str[index]

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 23
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript String Concatenation:

- Using ‘+’ Operator :


 In JavaScript the ‘+’ operator is also use to concat two or more String
 Example :

var str1 = "VJTech Academy";


var str2 = "No1 In Maharashtra";
document.write(str1 + str2 );

- Using concat() method :


 The concat() method in javascript is used to concatenate the string arguments such that the
resultant string is the concatenation of all the strings.
 Syntax :

str.concat(a);
str.concat(a, b);
str.concat(a, b, ... , z);

 Parameters
a, b, ..., z: on or more strings that are to be concatenated with str.
 Return value:
A new string containing the combined text of the strings provided in the argument.
 Example:

<body>
<script language="javascript" type="text/javascript">
var str1 = "VJTech Academy";
var str2 = "No1 In Maharashtra";
document.write(str1.concat(str2));
</script>
</body>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 24
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript String indexOf() Method :

- The indexOf() method takes a string value and returns the index of the first occurrence of the
passed value in the calling string.
- Syntax :

str.indexOf(val);
str.indexOf(val, index);

- Parameters :
 val: It specifies the string value that is to be searched for in the str.
 index (optional): By default the indexOf() method starts looking for the val from the index
0. This parameter is used to specify the starting index for the lookup.
- Return Value :
 It returns a number which is the index of the first appearance of val. If val is not present in
the string, then it returns -1.
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var str = 'Javascript is a programming language';
document.write(str.indexOf('s'));
//output: 4
document.write(str.indexOf('Z'));
//output: -1
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 25
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript String split() method:

- The split() method in javascript is used to divide the given string into an ordered list of
substrings.
- The string division is based on a pattern, where the pattern is provided as the first parameter
in the method's call.
- Syntax :

str.split();
str.split(pattern);
str.split(pattern, limit);

- Parameters :
 pattern (optional): The patterns define how the string split should occur. It could be a string
or a regular expression.
 limit (optional): It decides the number of substrings to be included in the array, i.e., once
the resultant array has reached the limit, the string split stops. Its value should be positive.
- Return Value :
 It returns an array of strings.
- Example :
<html>
<body>
<script language="javascript" type="text/javascript">
var str = 'Javascript is a programming language';
document.write(str.split(' '));
document.write("<br>");
//output: [ 'Javascript', 'is', 'a', 'programming', 'language' ]
document.write(str.split(' ', 2));
document.write("<br>");
//output: [ 'Javascript', 'is' ]
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 26
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript String subString() method:

- The javascript substring() method is used to get a substring from a string.


- It extracts the characters between two indices of a string and returns a substring.
- Syntax :

str.substring(begin);
str.substring(begin, end);

- Parameters :
 begin: It defines the index of the starting character of the substring.
 end (optional): It is the index of the first character of the string that should be excluded
from the substring.
- Return Value :
 A string containing a part of the parent string.
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var str = 'Javascript';
document.write(str.substring(3));
//output: ascript
document.write(str.substring(3, 7));
//output: ascr
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 27
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript String toLowerCase() method:

- The toLowerCase() method in javascript string converts all the characters of the string into
lowercase letters.
- Note: The toLowerCase() method doesn't affect the value of the original string but returns a
new string with lowercase characters.
- Syntax :

str.toLowerCase();

- Return Value :
 It returns a new string with all the characters converted into lowercase letters
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var str = 'Javascript Is a proGraMMing language';
var newStr = str.toLowerCase();
document.write(newStr);
//output: javascript is a programming language
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 28
VJ TECH ACADEMY – JAVASCRIPT NOTES

 JavaScript String toUpperCase() method:

- The toUpperCase() method in javascript string converts all the characters of the string into
uppercase letters.
- Note: The toUpperCase() method doesn't affect the value of the original string but returns a
new string with lowercase characters.

- Syntax :

str.toUpperCase();

- Return Value :
 It returns a new string with all the characters converted into uppercase letters
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var str = 'Javascript Is a proGraMMing language';
var newStr = str.toLowerCase();
document.write(newStr);
//output: JAVASCRIPT IS A PROGRAMMING LANGUAGE
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 29
VJ TECH ACADEMY – JAVASCRIPT NOTES

 Finding Unicode of Character :

- In this section, we can retrieve ASCII value of given character and also we can try to retrieve
character from given ASCII.
- To do this following methods are required :

1. charCodeAt(index) Method:

- The charCodeAt() method is used to retrieve ASCII value of given character.


- Unicode is a standard that assigns unique codes to characters from various writing systems.
- Syntax :
string.charCodeAt(index);

- Parameters :
 string: The string from which you want to retrieve the character code.
 index: The index of the character for which you want to get the Unicode value.

- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var text = "Hello";
var charCode = text.charCodeAt(1); // Returns the Unicode value of 'e'
document.write(charCode); // Outputs: 101
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 30
VJ TECH ACADEMY – JAVASCRIPT NOTES

2. fromCharCode(codePoint1, codePoint2, ...) Method:

- The fromCharCode() method is used to retrieve character from given ASCII value.
- For example, suppose given ASCII value is 65 then then by using above method we will get
its corresponding character 'A'.
- Syntax :
string.fromCharCode(codePoint1,codePoint2,…);

- Parameters :
 codePoint1 : code to retrieve char [ one or more]
- Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var char1 = String.fromCharCode(65); // Returns the character 'A'
var char2 = String.fromCharCode(97, 98, 99); // Returns the string 'abc'
document.write(char1); // Outputs: A
document.write("<br>");
document.write(char2); // Outputs: abc
document.write("<br>");
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 31
VJ TECH ACADEMY – JAVASCRIPT NOTES

 String Conversion :

- Converting between strings and numbers is a common operation in JavaScript.


- JavaScript provides methods and techniques to convert numbers to strings (toString()) and
strings to numbers (parseInt(), parseFloat(), and unary plus +).
-
1. Number to String Using ‘toString()’ Method :

 The toString() method converts a number to its string representation.


 Syntax :

number.toString();

 Example :

<html>
<body>
<script language="javascript" type="text/javascript">
var num = 42;
var str = num.toString(); // Converts to "42"
var binStr = num.toString(2); // Converts to binary "101010"
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 32
VJ TECH ACADEMY – JAVASCRIPT NOTES

2. String to Using ‘parseInt(), parseFloat() , Number() ’ Method :

1. parseInt() Method :
- This method will convert given string into integer number.
2. parseFloat() Method :
- This method will convert given string into float number.
3. Number() Method :
- This method is used to convert string into number.
- This method will work for complete number and decimal number.

Example :

<html>
<body>
<script language="javascript" type="text/javascript">
// Converting Strings to Numbers
var intStr = "123";
var parsedInt = parseInt(intStr); // Using parseInt()

var floatStr = "3.14";


var parsedFloat = parseFloat(floatStr); // Using parseFloat()

var numericString = "42";


var numericValue = Number(numericString); // Using Number()

document.write("String to Number Conversions:");


document.write("<br>");
document.write("parsedInt:", parsedInt); // Output: 123
document.write("<br>");
document.write("parsedFloat:", parsedFloat); // Output: 3.14
document.write("<br>");
document.write("numericValue:", numericValue); // Output: 42
</script>
</body>
</html>

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 33
VJ TECH ACADEMY – JAVASCRIPT NOTES

VJTECH ACADEMY
ClAss noTEs

JavaScript Notes by Vishal Jadhav Sir (VJTech Academy, contact us: +91-7743909870 ) Page 34

You might also like