Introduction - potatoscript/javascript GitHub Wiki
JavaScript is like the magic behind making websites interactive! When you visit a webpage and see things happening (like buttons that click, menus that open, or pictures that change), itโs likely JavaScript at work. It's a programming language used by web developers to make websites come to life.
Think of JavaScript as the tool that helps websites think and react. Just like you can talk and interact with people, JavaScript helps your website talk and interact with people too. ๐ค๐
Imagine youโre playing a game online, and you click a button to score points. Without JavaScript, the game would just show a static image, like a picture of the game. But with JavaScript, the game can update the score automatically, show animations, and respond to your clicks in real-time! It brings life to the web. ๐ฎ๐ป
- Without JavaScript: Just a picture.
- With JavaScript: An interactive game that responds to your clicks!
When you visit a website, your browser (like Google Chrome or Firefox) sends a request to the website's server. The server sends back the HTML, CSS, and JavaScript files that tell the browser how the page should look and work. JavaScript helps control what happens on the page while you interact with it.
๐ HTML: Makes the structure (like headings and paragraphs). ๐จ CSS: Makes the page look nice (like colors and fonts). โก JavaScript: Makes the page interactive (like clicking buttons and moving pictures).
- Online games: When you play games on websites.
- Social media: When you scroll, like, or comment on posts, JavaScript makes it smooth and interactive.
- Shopping websites: When you add things to your cart or see pop-up messages.
In all these places, JavaScript is quietly working in the background, making things happen! ๐ป๐๏ธ๐ฎ
We add JavaScript to websites using <script> tags. These tags tell the browser, "Hey! Here's some JavaScript, please run it."
Hereโs the simplest JavaScript you can run to say "Hello, World!" on your browser's console. The console is like a secret chat between the browser and the developer.
- Open any web page.
- Press F12 to open the Developer Tools.
- Go to the Console tab.
- Type the following code:
console.log("Hello, World!");When you press Enter, youโll see "Hello, World!" appear in the console.
๐ Congratulations! Youโve written your very first line of JavaScript! ๐
Hereโs how you write JavaScript in a web page:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>JavaScript Example</title>
</head>
<body>
<h1>Welcome to JavaScript!</h1>
<button onclick="changeColor()">Click Me!</button>
<script>
// This is where we add our JavaScript code
// Function to change the background color
function changeColor() {
document.body.style.backgroundColor = "lightblue";
}
</script>
</body>
</html>-
HTML: This part creates the structure of your webpage. For example, the
<button>creates a button that users can click. -
JavaScript: The script inside
<script>tags contains the magic! When you click the button, it changes the background color of the page to light blue. โจ
-
<button onclick="changeColor()">: This creates a button, andonclick="changeColor()"means, "When the user clicks the button, run thechangeColorfunction." -
document.body.style.backgroundColor = "lightblue";: This is the JavaScript code that changes the background color of the webpage to light blue when the button is clicked.
Hereโs a simple breakdown of some important things youโll see in JavaScript code:
-
Variables ๐ฆ: Used to store information.
- Example:
let color = "blue";
- Example:
-
Functions ๐ฏ: A set of instructions that do something when called.
- Example:
function sayHello() { alert("Hello, World!"); }
- Example:
-
Events ๐ฌ: Things that happen, like when you click a button.
- Example:
onclick,onmouseover,onkeydown
- Example:
-
Operators โ: Used to perform actions, like adding numbers or comparing values.
- Example:
let sum = 5 + 3;
- Example:
When a browser sees a websiteโs code, it first reads the HTML and CSS. Then, it sees the JavaScript and runs the instructions to make the page interactive.
- HTML creates the page.
- CSS makes the page look good.
- JavaScript makes the page work and react to your actions! ๐
- ๐ป Computer: Represents the browser that reads the code.
- ๐๏ธ Pencil: Represents writing JavaScript code.
- โก Lightning Bolt: Represents the dynamic behavior JavaScript adds to websites.
- ๐ฎ Game Controller: Represents the interactivity JavaScript brings.
- ๐ Globe: Represents the world wide web, where JavaScript works!
Now that youโve learned what JavaScript is and how it works, the next steps involve learning how to:
- Create variables to store data.
- Write functions to make things happen when you click a button or interact with a webpage.
- Manipulate HTML elements to change things on the page when something happens.
- Handle events like clicks, keyboard presses, and more.