Loading repository data…
Loading repository data…
olmescode / repository
Descubre lo esencial de JavaScript en este repo para principiantes. Tipos de datos, funciones y más de forma simple.
JavaScript is a versatile, high-level, interpreted programming language primarily used for client-side web development. It enables interactive and dynamic features on websites, making them more engaging for users. Developed in the mid-'90s, JavaScript has evolved into a key technology for building modern web applications.
Variables are memory spaces where we can store information, depending on the types and data structures supported by our language.
Declaring is telling JavaScript we're creating a variable with a specific name. Initializing is giving that variable a value.
The + operator allows us to add numbers and concatenate strings. When used with numbers, it adds them. With strings, it joins (concatenates) them.
Reserved word for a variable, depending on the case, can be:
varletconstvar my_variable = "Declare variable";
Variable name: var my_variable
Assignment operator: =
Data assigned to the variable: "Declare variable"
Number:
JavaScript handles both integers and decimals as numbers.
let age = 40;
let temperature = 25.5;
String:
Strings can hold anything from a single character to a whole paragraph.
let name = "Luis";
let greeting = "Hello, world!";
Boolean:
Booleans are great for conditions, like checking if it's sunny or raining.
let isSunny = true;
let isRaining = false;
Null and Undefined:
let nothing = null; // You might use this when you intentionally want to say, "There's nothing here."
let notDefined; // If you declare a variable but don't assign a value, it's undefined.
Array:
Arrays can store multiple values, making it easy to work with lists of items.
let favoriteFruits = ["Apple", "Banana", "Orange"];
Object:
Objects help you organize data with named properties. Here, we have a person with a name, age, and student status.
let myObject = {
name: "Luis",
age: 20,
isStudent: true
};
Functions:
Functions are a type of data in JavaScript. They can be assigned to variables and passed around in your code.
let greet = function() {
console.log("Hello!");
};
Date:
The Date type is used for working with dates and times.
let currentDate = new Date();
RegExp (Regular Expression):
Regular expressions are used for pattern matching within strings.
let pattern = /[a-zA-Z]+/;
let name = 'Juan Sultan';
let lastName = 'Galeas Zabala';
let username = 'juandc';
let age = 19;
let email = 'crespo@gmail.xyz';
let isLegalAge = true;
let savedMoney = 1000;
let debts = 100;
let fullName = name + ' ' + lastName;
let realMoney = savedMoney - debts;
A function is like a recipe – a set of instructions to perform a specific task.
Use functions to avoid repeating code, make it more readable, and organize tasks logically.
Parameters are placeholders in a function definition, and arguments are the actual values passed when calling the function.
function introduceYourself(name, lastName, nickname) {
let completeName = name + ' ' + lastName;
console.log("My name is " + completeName + ", but I prefer you to call me " + nickname + ".");
}
A conditional is a decision-making structure in code.
In JavaScript, there are if, else if, and else. They handle different conditions sequentially.
Absolutely, you can use functions to encapsulate tasks within conditionals.
const subscriptionType = "Basic";
if (subscriptionType === "Free") {
console.log("You can only take free courses.");
} else if (subscriptionType === "Basic") {
console.log("You can take almost all Platzi courses for a month.");
} else if (subscriptionType === "Expert") {
console.log("You can take almost all Platzi courses for a year.");
} else if (subscriptionType === "ExpertPlus") {
console.log("You and someone else can take ALL Platzi courses for a year.");
}
/*
"lookup table" or "dictionary."
It's essentially a mapping between keys (subscription types) and values (associated messages).
This approach leverages the key-based retrieval capabilities of objects, providing a cleaner and more efficient way to manage multiple conditions.
*/
// Subscription information object
const subscriptionInfo = {
Free: "You can only take free courses.",
Basic: "You can take almost all Platzi courses for a month.",
Expert: "You can take almost all Platzi courses for a year.",
ExpertPlus: "You and someone else can take ALL Platzi courses for a year."
};
// Function to check and display subscription information
function checkSubscription(subscriptionType) {
const message = subscriptionInfo[subscriptionType];
if (message) {
console.log(message);
} else {
console.log("Invalid subscription type.");
}
}
// Example usage:
checkSubscription("Free");
checkSubscription("Basic");
checkSubscription("Expert");
checkSubscription("ExpertPlus");
checkSubscription("InvalidType");
A loop is a way to repeatedly execute a block of code.
Common loops are for, while, and do-while.
An infinite loop never stops, causing the program to run indefinitely, often leading to system or application crashes.
Absolutely, combining loops and conditionals is common for complex logic.
for loops to while:// for loop
for (let i = 0; i < 5; i++) {
console.log("El valor de i es: " + i);
}
for (let i = 10; i >= 2; i--) {
console.log("El valor de i es: " + i);
}
// while loop
let i = 0;
while (i < 5) {
console.log("The value of i is: " + i);
i++;
}
let j = 10;
while (j >= 2) {
console.log("The value of j is: " + j);
j--;
}
let userAnswer;
do {
userAnswer = prompt("What is 2 + 2?");
} while (userAnswer !== "4");
alert("Congratulations! You got it right!");
An array is a data structure that stores a collection of elements.
An object is a collection of key-value pairs, often representing real-world entities.
Use arrays for ordered lists of similar items; use objects for key-value pairs representing attributes of a single entity.
Absolutely, you can have arrays of objects or objects containing arrays.
let myArray = [1, 2, 3, 4];
function printFirstElement(arr) {
console.log(arr[0]);
}
printFirstElement(myArray);
let anotherArray = ['apple', 'orange', 'banana'];
function printAllElements(arr) {
for (let element of arr) {
console.log(element);
}
}
printAllElements(anotherArray);
function printAllObjectElements(obj) {
for (let key in obj) {
console.log(obj[key]);
}
}
// Example usage:
let myObject = { key1: 'value1', key2: 'value2', key3: 'value3' };
printAllObjectElements(myObject);