JavaScript is a widely-used programming language for building interactive websites and web applications. It is primarily used for client-side development, meaning it runs on the user’s web browser.
Getting started with JavaScript
To start using JavaScript in your web page, you can include the <script>
tag in your HTML code. Here’s an example:
<!DOCTYPE html>
<html>
<head>
<title>My First JavaScript Page</title>
</head>
<body>
<h1>JavaScript Demo</h1>
<script>
// Your JavaScript code here
alert("Hello, JavaScript!");
</script>
</body>
</html>
In the example above, the JavaScript code within the <script>
tag is executed when the web page is loaded, and it displays a popup alert with the message “Hello, JavaScript!”.
Variables and Data Types
In JavaScript, variables are used to store data. You can declare a variable using the var
, let
, or const
keyword. Here’s an example:
var myVariable = 10; // Number
let myString = "Hello, JavaScript!"; // String
const PI = 3.14; // Constant
JavaScript has various data types including numbers, strings, booleans, arrays, objects, and more.
Control Flow and Loops
JavaScript provides control flow statements like if
, else if
, else
to perform conditional operations. Here’s an example:
let age = 25;
if (age < 18) {
console.log("You are a minor.");
} else if (age >= 18 && age < 65) {
console.log("You are an adult.");
} else {
console.log("You are a senior citizen.");
}
JavaScript also has loop structures like for
and while
for repeated execution of code blocks. Here’s an example of a for
loop:
for (let i = 0; i < 5; i++) {
console.log(i);
}
This will output the numbers from 0 to 4.
Conclusion
This is just a basic introduction to JavaScript. There’s much more to learn and explore in JavaScript. I recommend referring to the Mozilla Developer Network (MDN) for detailed documentation and tutorials on JavaScript. Happy coding!