BASIC SYNTAX OF JAVASCRIPT
Variables: Variables are like containers that hold information. They can store numbers, words, or other types of data.
var number = 5;
var greeting = "Hello!";Functions: Functions are like small programs. That tells the computer to do a specific task, and it follows the instructions.
function addNumbers(a, b)
addNumbers(3, 4);
{
return a + b;
}
var result =Conditions: We can make the computer decide what to do based on certain conditions. For example, if a number is greater than 0, do one thing; otherwise, do something else.
var number = 5;
if (number > 0)
{
console.log("Positive number");
}
else
{
console.log("Negative number");
}Loops: Loops help us to repeat tasks. If we want to do something several times, we can use a loop.
for (var i = 0; i < 5; i++)
{
console.log(i);
}Objects: Objects are like containers that can hold multiple pieces of information. Each piece is called a property.
var person = { name: "John", age: 30, isStudent: false, };
Arrays: Arrays are like lists where you can store multiple items.
var colors = ["red", "green", "blue"];
Event Handling: You can make things happen on a webpage in response to user actions, like clicking a button.
document.getElementById("myButton").addEventListener("click", function() { console.log("Button clicked!"); });
Comments: Comments are notes in the code for us or others to understand what's happening. They don't affect how the code runs.
// This is a comment
Comments
Post a Comment