Here are some basic JavaScript codes to get you started:
- Hello World:
javascriptconsole.log("Hello, World!");
- Variables and data types:
javascript// Declaring variables
var x = 5;
let y = "Hello";
const z = true;
// Data types
console.log(typeof x); // number
console.log(typeof y); // string
console.log(typeof z); // boolean
- Conditional statements:
javascriptlet age = 20;
if (age < 18) {
console.log("You are not old enough to vote");
} else if (age >= 18 && age <= 65) {
console.log("You are eligible to vote");
} else {
console.log("You are above the voting age limit");
}
- Loops:
javascript// For loop
for (let i = 0; i < 5; i++) {
console.log(i);
}
// While loop
let j = 0;
while (j < 5) {
console.log(j);
j++;
}
- Functions:
javascript// Function declaration
function addNumbers(a, b) {
return a + b;
}
// Function expression
let multiplyNumbers = function(a, b) {
return a * b;
}
// Arrow function
let divideNumbers = (a, b) => {
return a / b;
}
console.log(addNumbers(2, 3)); // 5
console.log(multiplyNumbers(2, 3)); // 6
console.log(divideNumbers(6, 2)); // 3
These are just some basic examples of JavaScript codes. As you continue to learn and practice, you can explore more advanced concepts like object-oriented programming, array methods, and APIs.
Comments
Post a Comment