JavaScript Code Examples

JavaScript Code Examples

Variables:

let name = "John Doe";

let age = 30;

let isStudent = false;

console.log(name);

console.log(age);

console.log(isStudent);


Explanation: In this code, we are declaring 3 variables using the let keyword. The let keyword allows us to declare variables in JavaScript. The first variable name is assigned a string value of "John Doe". The second variable age is assigned a number value of 30. The third variable isStudent is assigned a boolean value of false. Finally, we log the values of these variables to the console using the console.log method.


Arrays:

let names = ["John", "Jane", "Jim"];

console.log(names[0]);

console.log(names.length);

names.push("Jake");

console.log(names);


Explanation: In this code, we are declaring an array named names that contains three string values "John", "Jane", and "Jim". The first console.log statement logs the first element of the array to the console, which is "John". The second console.log statement logs the length of the array to the console, which is 3. The push method is then used to add another element "Jake" to the end of the array. The final console.log statement logs the updated array to the console.


Example : Array

let fruits = ["apple", "banana", "cherry"];

for (let i = 0; i < fruits.length; i++) {

??console.log("I like " + fruits[i]);

}


Explanation: This example demonstrates the use of an array. We create an array fruits containing three strings "apple", "banana", and "cherry". We then use a for loop to iterate over each element in the array and log a message to the console using console.log(). This will log the message "I like apple", "I like banana", and "I like cherry" to the console.


Example: Object

let car = {

??make: "Toyota",

??model: "Camry",

??year: 2022,

};


console.log("I drive a " + car.year + " " + car.make + " " + car.model);


Explanation: This example demonstrates the use of an object. We create an object car with three properties make, model, and year. We then use dot notation


Objects:

let person = {

??name: "John Doe",

??age: 30,

??isStudent: false

};

console.log(person.name);

console.log(person.age);

console.log(person.isStudent);


Explanation: In this code, we are declaring an object named person with properties name, age, and isStudent. The properties of the object store values "John Doe", 30, and false respectively. The console.log statements log the values of the properties to the console.


Conditional Statements:

let grade = 75;

if (grade >= 60) {

??console.log("Passed");

} else {

??console.log("Failed");

}


Explanation: In this code, we are declaring a variable grade with a value of 75. We then use an if...else statement to determine whether the student has passed or failed based on their grade. If the value of grade is greater than or equal to 60, the code inside the first block (i.e., console.log("Passed")) will be executed, and the message "Passed" will be logged to the console. If the value of grade is less than 60, the code inside the second block (i.e., console.log("Failed")) will be executed, and the message "Failed" will be logged to the console.


Functions:

function greet(name) {

??console.log("Hello, " + name);

}

greet("John");


Explanation: In this code, we are defining a function named greet that takes a parameter name. The function logs a greeting message to the console by concatenating the string "Hello, " with the value of the name parameter. The function is then called by passing a string argument "John" to it, which results in the message "Hello, John" being logged to the console.


Example: Simple Function

function greet(name) {

??console.log("Hello " + name);

}


greet("John");


Explanation: In this example, we have defined a simple function greet that takes a single argument name and logs a greeting to the console using console.log(). To call the function, we pass in an argument, such as "John", to the function and invoke it. This will log the message "Hello John" to the console.


Example: Conditional Statement

let num = 5;

if (num > 0) {

??console.log(num + " is a positive number");

} else {

??console.log(num + " is a negative number");

}

Explanation: This example demonstrates the use of a conditional statement using an if statement. The code checks if the value of num is greater than 0. If it is, the code inside the first set of curly braces is executed, and the message "5 is a positive number" is logged to the console. If the value of num is not greater than 0, the code inside the else block is executed, and the message "5 is a negative number" is logged to the console.


String Methods:

let message = "Hello World";

console.log(message.toUpperCase());

console.log(message.includes("Hello"));


Explanation: In this code, we are declaring a variable message with a string value of "Hello World". The first console.log statement logs the result of calling the toUpperCase method on the message string, which returns an uppercase version of the string: "HELLO WORLD". The second console.log statement logs the result of calling the includes method on the message string, which checks if the string "Hello" is a part of the message string. Since it is, the method returns true, which is then logged to the console.


Ternary Operators:

let grade = 75;

let result = (grade >= 60) ? "Passed" : "Failed";

console.log(result);


Explanation: In this code, we are declaring a variable grade with a value of 75 and using a ternary operator to assign a value to the result variable based on the value of grade. The expression (grade >= 60) ? "Passed" : "Failed" says that if grade is greater than or equal to 60, the value of result should be "Passed", otherwise it should be "Failed". The final console.log statement logs the value of result to the console.


Using if statements:

let age = 25;

if (age >= 18) {

??console.log("You are an adult.");

} else {

??console.log("You are not an adult.");

}


Explanation: In this code, we are declaring a variable age with the value 25. We are then using an if statement to determine if the value of age is greater than or equal to 18. If it is, the first console.log statement is executed, logging "You are an adult." to the console. If not, the else block is executed, logging "You are not an adult." to the console.


Using the switch statement:

let day = 2;

switch (day) {

??case 1:

????console.log("Monday");

????break;

??case 2:

????console.log("Tuesday");

????break;

??case 3:

????console.log("Wednesday");

????break;

??default:

????console.log("Invalid day");

}


Explanation: In this code, we are using a switch statement to control the flow of the program based on the value of a variable. The switch statement takes an expression as an argument and compares it to the case labels. If a match is found, the code inside the corresponding case block is executed. If no match is found, the code inside the default block is executed. In this case, the value of day is 2, so the code inside the case 2 block is executed and the result "Tuesday" is logged to the console.


Example of switch statements:

let grade = "A";

switch (grade) {

??case "A":

????console.log("Excellent");

????break;

??case "B":

????console.log("Good");

????break;

??case "C":

????console.log("Average");

????break;

??default:

????console.log("Invalid grade");

????break;

}


Explanation: In this code, we are declaring a variable grade with the value "A". We are then using a switch statement to match the value of grade with different cases. If the value of grade is "A", the first console.log statement is executed, logging "Excellent" to the console. If the value of grade is "B", the second console.log statement is executed, logging "Good" to the console. If the value of grade is "C", the third console.log statement is executed, logging "Average" to the console. If the value of grade does not match any of the cases, the default block is executed, logging "Invalid grade" to the console.


Using an object to store data:

let person = {

??name: "John",

??age: 30,

??occupation: "Teacher"

};

console.log(person.name);

Explanation: In this code, we are using an object to store data about a person. An object is a collection of key-value pairs, and in this case, the keys are name, age, and occupation. The values are the corresponding data for each key. To access the data stored in an object, we use dot notation, such as person.name to access the value of the name key. The value of person.name is logged to the console using the console.log() method.

要查看或添加评论,请登录

社区洞察

其他会员也浏览了