JavaScript Introduction
JavaScript is a powerful programming language used to create dynamic and interactive web pages. It allows developers to add functionality such as animations, form validation, events, and much more.
What is JavaScript?
JavaScript is a scripting language that runs in the browser. It works together with HTML and CSS to build modern web applications.
- HTML defines the structure of a web page
- CSS defines the design and layout
- JavaScript adds interactivity and behavior
Why Learn JavaScript?
- It is essential for web development
- It makes websites interactive
- It is supported by all modern browsers
- It is used in both front-end and back-end development
JavaScript Example
In the following example, a button is connected to a JavaScript function. When the button is clicked, the function executes and displays a message using an alert box. This demonstrates how JavaScript can handle user events and add interactivity to a web page.
<!DOCTYPE html> <html> <body> <button onclick="showMessage()">Click Me</button> <script> function showMessage() { alert("Hello, JavaScript!"); } </script> </body> </html>Try this code
JavaScript in HTML
JavaScript code can be written inside HTML using the <script> tag. It can be placed in the <head> or <body> section.
<script>
console.log("Hello World");
</script>
External JavaScript
JavaScript can also be written in a separate file and linked to an HTML document. This helps keep code organized and reusable.
<script src="script.js"></script>
JavaScript Output
JavaScript can display output in different ways:
- Using alert() to show popup messages
- Using console.log() for debugging
- Writing content directly to the HTML page
JavaScript Can Change HTML Content
JavaScript can be used to dynamically change the content of HTML elements.
In the example below, when the user clicks the button, JavaScript selects
the paragraph using its id and updates its text using the innerHTML property.
This allows web pages to respond instantly to user actions.
<!DOCTYPE html> <html> <body> <p id="demo">Javascript can change html content</p> <button type="button" onclick='document.getElementById("demo").innerHTML = "Hello JavaScript!"'>Click Me</button> </body> </html>Try this code
JavaScript Can Change CSS
JavaScript can also be used to change the style of HTML elements dynamically.
In the example below, when the user clicks the button, JavaScript selects
the paragraph and changes its text color using the style property.
This allows developers to modify the appearance of elements in real time.
<!DOCTYPE html> <html> <body> <p id="text">Styled Text</p> <button type="button" onclick='document.getElementById("text").style.color = "red"' >Click Me</button> </body> </html>Try this code
JavaScript Use Cases
- Form validation
- Image sliders
- Dropdown menus
- Interactive maps
- Games and animations
Why JavaScript is Important
- It makes websites interactive
- It improves user experience
- It is widely used in modern web development
- It works with frameworks like React and Angular