JAVASCRIPT
What is JavaScript
- 1. JavaScript is a scripting language.
- 2. It is used to create dynamic and interactive web pages.
- 3. Runs in the browser (client-side) and also on servers using Node.js.
- 4. Can manipulate HTML/CSS, handle events, and control browser behavior.
- 5. Supports object-oriented and functional programming.
- 6. Commonly used with HTML and CSS in web development.
Example:
<!Doctype html> <html> <body> <p id="demo">Click the button!</p> <button onclick="changeText()">Click Me</button> <script> function changeText() { document.getElementById("demo").innerHTML = "Hello, JavaScript!"; } </script> </body> </html>
Types of JavaScript in terms of usage in HTML:
1. Inline JavaScript:
JavaScript code can be added directly within an HTML element using attributes like onclick and onmouseover.
Example:
<button onclick="alert('Hello!')">Click Me</button>
2. Internal JavaScript:
JavaScript is typically placed inside <script> tags within the same HTML file. You can easily use this tag in either the head or body sections.
Example:
<!Doctype html> <html> <head> <title>Internal JS </title> <script> function greet(){ alert("Welcome JavaScript"); } </script> </head> <body> <button onclick="greet()"> Click Me </button> </body> </html>
3. External JavaScript:
JavaScript is typically stored in a separate .js file, which you can link to using the <script src="script.js"> tag. You can place this tag either in the head or the body of your HTML document.
Example:
Script.js:
function showmessage(){ alert("This is external JavaScript!"); }
Html File:
<!Doctype html> <html> <head> <title>External JS </title> <script src="script.js"></script> </head> <body> <button onclick="showmessage()"> Click Me </button> </body> </html>
Comment and Types of comments in JavaScript:
- JavaScript supports two types of comments.
1. Single-line Comment:
- a. Starts with //
- b. Anything after // on that line is ignored by the JavaScript engine.
Example:
// This is a single line comment
2. Multi-line Comment:
- a. Starts with /* and ends with */
- b. Can span across multiple lines.
Example:
/* This is a multi - line comment. It can describe logic or disable block of code. */ let age = 25;
No comments:
Post a Comment