JavaScript
JavaScript is a versatile(client side and server-side) programming language commonly
used for building interactive websites.
If you're a beginner, here are some fundamental concepts and examples to help you get started
with JavaScript:
1. Variables and Data Types:
Variables are used to store data values.
You can create a variable using the var, let, or const keyword.
// Variable declaration
var myVariable = 10;
// Data types
var number = 42; // Number
var text = "Hello"; // String
var isTrue = true; // Boolean
var array = [1, 2, 3]; // Array
var object = { // Object
key: "value",
anotherKey: 123
};
2. Operators:
JavaScript supports various operators for performing operations on variables.
var sum = 5 + 3;
var difference = 10 - 5;
var product = 2 * 4;
var quotient = 8 / 2;
var remainder = 10 % 3;
3. Control Flow:
Use if, else if, and else for conditional statements.
var num = 10;
if (num > 0) {
[Link]("Positive number");
} else if (num < 0) {
[Link]("Negative number");
} else {
[Link]("Zero");
}
4. Loops:
for and while loops are used for repetitive tasks.
// For loop
for (var i = 0; i < 5; i++) {
[Link](i);
}
// While loop
var count = 0;
while (count < 5) {
[Link](count);
count++;
}
5. Functions:
Functions allow you to group code into reusable blocks.
// Function declaration
function greet(name) {
[Link]("Hello, " + name + "!");
}
// Function call
greet("Alice");
6. Arrays:
Arrays store multiple values in a single variable.
var fruits = ["Apple", "Orange", "Banana"];
// Accessing elements
[Link](fruits[0]); // Output: Apple
// Modifying elements
[Link]("Grapes");
7. Objects:
Objects allow you to group data and functions together.
var person = {
name: "John",
age: 25,
greet: function() {
[Link]("Hello, I'm " + [Link] + "!");
}
};
[Link]();
8. DOM Manipulation:
JavaScript is often used to interact with the Document Object Model (DOM) to change the
content of HTML elements.
<!-- HTML -->
<button me</button>
<p id="demo">Hello, World!</p>
<!-- JavaScript -->
<script>
function changeText() {
[Link]("demo").innerHTML = "Hello, JavaScript!";
}
</script>