Java Intro [Autosaved] [Autosaved]1
Java Intro [Autosaved] [Autosaved]1
JavaScript
Introduction to JavaScript
What is JavaScript?
JavaScript (JS) is a programming
language used to make web pages
interactive.
It runs directly in web browsers.
Developed in 1995 by Brendan Eich.
What is a Variable?
A variable is like a container that holds data. In JavaScript, you can
store text, numbers, objects, or other data types inside variables.
JavaScript gives you three ways to create a variable:
var name = "John";
let age = 25;
const PI = 3.14159;
An alert() is a built-in function in JavaScript that shows a popup message on the screen.
function
greet();
alert("Welcome to my website!");
event handler
Every one have to create a three buttons and make the buttons
interactive with lesson learned
Assigment
I want everyone to create a p tag element and write some text to it and
finally create other button and when that button is clicked the p text
needs to change ?
The DOM (Document Object Model) in JavaScript is a
programming interface that represents the structure of an HTML
or XML document as a tree of objects. It allows JavaScript to
dynamically access, manipulate, and update the content,
structure, and styles of a webpage.
1. innerHTML
•Returns or sets the HTML content inside an element.
•Parses and interprets HTML tags inside the string.
•Can be used to insert elements dynamically.
•textContent
•Returns or sets only the text content inside an
element.
•Ignores and does not parse HTML tags.
<script>
// Using innerHTML
document.getElementById("para1").innerHTML = "This is
<b>bold</b> text.";
// Using textContent
document.getElementById("para2").textContent = "This is
<b>bold</b> text.";
</script>
document.getElementById() in JavaScript
The document.getElementById() method is used to select an HTML element by its id and manipulate it using
JavaScript. It is one of the most common ways to interact with elements in the DOM (Document Object Model).
Syntax:
javascript
document.getElementById("elementID");
•"elementID" is the id of the HTML element you want to access.
•Returns a reference to the element or null if not found.
JavaScript Can Change HTML Styles (CSS)
Changing the style of an HTML element, is a variant of changing an
HTML attribute:
Example
document.getElementById("demo").style.fontSize = "35px";
<script>
document.getElementById("demo").innerHTML = "My First JavaScript";
</script>
<html>
<head>
<script>
function myFunction() {
document.getElementById("demo").innerHTML = "Paragraph changed.";
}
</script>
</head>
<body><h2>Demo JavaScript in Head</h2>
function is used to display a dialog box that asks the user for input. It
provides a simple way to collect text input from users.
let userInput = prompt("Enter your name:");
let name=parseFloat(prompt('enter a number'))
let name=parseInt(prompt('enter a number'))
document.getElementById("ruru").value
javaScript template literals (using backticks ` `) allow placeholders inside strings using
${}.
console.log(message);
Logical Operators in JavaScript
Logical operators are used to combine or manipulate boolean values (true or false). They are mainly
used in conditional statements and loops.
1. AND (&&)
•Returns true if both conditions are true, otherwise false.
•Example:
javascript
let a = 10, b = 20; console.log(a > 5 && b < 30); // true (both conditions are true) console.log(a > 15 && b < 30); // false (first
condition is false)
OR (||)
•Returns true if at least one condition is true.
•Example:
let x = 5, y = 15; console.log(x > 10 || y < 20); // true (one condition is true) console.log(x > 10 || y >
20); // false (both conditions are false)