What is JavaScript
What is JavaScript
HTML JavaScript
HTML (Hypertext Markup Language) is a JavaScript is a programming language used for
markup language used for creating static creating interactive and dynamic web pages.
web pages.
HTML is used to define the structure and JavaScript is used to add interactivity, modify
content of a web page. the content and styling of a web page, and
handle user events.
HTML is declarative and does not have JavaScript is a scripting language and can
the ability to perform logic or make perform logic, make decisions, and manipulate
decisions. the HTML DOM.
HTML consists of tags surrounded by JavaScript is written in a script and uses syntax
angle brackets. similar to other programming languages.
HTML documents are static and do not JavaScript can update and change the content
change unless modified by a developer. of an HTML document dynamically.
HTML is primarily used for structuring JavaScript is used for creating dynamic and
and displaying content. interactive experiences on the web.
HTML is not case sensitive. JavaScript is case sensitive.
HTML runs on the client side. JavaScript can run on both the client and server
side.
JavaScript Syntax
<script language=”javascript”>
Code….
</script>
</body>
</html>
document.write????
document.write in JavaScript is a function that is used to display some strings in
the output of HTML web pages (Browser window).
example
document.write("Hello World!");
document.write("<br>");
document.write("Have a nice day!");
JavaScript Variables
JavaScript variables are containers for storing data values.
Example
var x = 5;
var y = 6;
var z = x + y;
// Strings:
let color = "Yellow";
let lastName = "Johnson";
// Booleans
let x = true;
let y = false;
// Object:
const person = {firstName:"John", lastName:"Doe"};
// Array object:
const cars = ["Saab", "Volvo", "BMW"];
// Date object:
const date = new Date("2022-03-25");
JavaScript Functions
A JavaScript function is defined with the function keyword, followed by a name, followed by
parentheses ().
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
Function Return
When JavaScript reaches a return statement, the function will stop executing.
If the function was invoked from a statement, JavaScript will "return" to execute the code after the
invoking statement.
Functions often compute a return value. The return value is "returned" back to the "caller":
Example
function myFunction(a, b) {
// Function returns the product of a and b
return a * b;
}
function factorial(number) {
if (number === 0 || number === 1) {
return 1;
}
else {
let result = 1;
let i = 2;
while (i <= number) {
result *= i;
i++;
}
return result;
}
}
JavaScript Arrays
An array is a special variable, which can hold more than one value:
var cars = ["Saab", "Volvo", "BMW",”Maruti”];
Creating an Array
Syntax:
var array_name = [item1, item2, ...];
Example
Var cars = ["Saab", "Volvo", "BMW"];
Spaces and line breaks are not important. A declaration can span multiple lines:
Example
var cars = [
"Saab",
"Volvo",
"BMW"
];
You can also create an array, and then provide the elements:
Example
var cars = [];
cars[0]= "MAruti";
cars[1]= "Volvo";
cars[2]= "BMW";
Accessing Array Elements
var cars = ["Maruti", "Volvo", "BMW"];
var car = cars[0];
Access the Full Array
Using for….
Associative Arrays
Arrays with named indexes are called associative arrays (or hashes).
Example:
const person = [];
person["firstName"] = "John";
person["lastName"] = "Doe";
person["age"] = 46;
person.length; // Will return 0
person[0]; // Will return undefined
The charAt() method returns the character at a specified index (position) in a string:
Example
let text = "HELLO WORLD";
let char = text.charAt(6);
The charCodeAt() method returns the code of the character at a specified index in a string:
The method returns a UTF-16 code (an integer between 0 and 65535).
Example
let text = "HELLO WORLD";
let char = text.charCodeAt(0);
The at() method returns the character at a specified index (position) in a string.
The at() method is supported in all modern browsers since March 2022:
JavaScript String Search
JavaScript String indexOf()
The indexOf() method returns the index (position) of the first occurrence of a string in a string, or it
returns -1 if the string is not found:
Example
let text = "Please locate where 'locate' occurs!";
let index = text.indexOf("locate");
JavaScript String lastIndexOf()
The lastIndexOf() method returns the index of the last occurrence of a specified text in a string:
Example
let text = "Please locate where 'locate' occurs!";
let index = text.lastIndexOf("locate");
The search() method searches a string for a string (or a regular expression) and returns the position of
the match:
Examples
let text = "Please locate where 'locate' occurs!";
text.search("locate");
JavaScript String match()
The match() method returns an array containing the results of matching a string against a string (or a
regular expression).
Examples
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
The slice() Method
Hello
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The slice() Method</h2>
<p id="demo"></p>
<script>
let text = "Hello world!";
let result = text.slice(3);
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
lo world!
<html>
<body>
<h1>JavaScript Strings</h1>
<script>
</script>
</body>
</html>
replace() searches a string for a value, and returns a new string with the specified value(s) replaced:
Visit W3Schools!
JavaScript String replaceAll()
<html>
<body>
<h1>JavaScript Strings</h1>
<p id="demo"></p>
<script>
let text = "I love cats. Cats are very easy to love. Cats are very popular."
text = text.replaceAll("Cats","Dogs");
text = text.replaceAll("cats","dogs");
document.getElementById("demo").innerHTML = text;
</script>
</body>
</html>
The replaceAll() Method
I love dogs. Dogs are very easy to love. Dogs are very popular.
String indexOf()
<html>
<body>
<h1>JavaScript Strings</h1>
<p>Find "welcome":</p>
<p id="demo"></p>
<script>
document.getElementById("demo").innerHTML = result;
</script>
</body>
</html>
The indexOf() Method
Find "welcome":
13
Examples
Search a string for "welcome":
let text = "Hello world, welcome to the universe.";
let result = text.indexOf("welcome");
Example:
<!DOCTYPE html>
<html>
<body>
<h1>JavaScript Strings</h1>
<h2>The indexOf() Method</h2>
<p>Find "a":</p>
<p id="demo"></p>
<script>
let text = "Hello world, welcome to the universe.";
document.getElementById("demo").innerHTML = text.indexOf("a", 5);
</script>
</body>
</html>