Created
May 15, 2023 14:56
-
-
Save scastiel/22b84784b040c49b3cd6b429f23d55fe to your computer and use it in GitHub Desktop.
JavaScript basics
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
console.log("Hello World!"); | |
// --------------- Variables --------------- | |
let count = 12; | |
console.log(count); | |
count = count + 1; | |
console.log(count); | |
const firstName = 'John'; | |
console.log(firstName); | |
// firstName = 'Jane'; | |
// TypeError: Assignment to constant variable. | |
// --------------- Strings --------------- | |
console.log(firstName.length); | |
console.log(firstName[1]); | |
console.log(firstName.substring(1, 3)); | |
console.log(firstName.toUpperCase()); | |
console.log(firstName.toLowerCase()); | |
const daysString = 'Mon,Tue,Wedm,Thu,Fri,Sat,Sun' | |
const days = daysString.split(',') | |
console.log(days.length) | |
const first = "Ringo"; | |
const last = "Starr"; | |
const message = `${first} ${last} is a drummer`; | |
console.log(message); | |
// --------------- Arrays --------------- | |
const fruits = []; | |
fruits.push("Apple"); | |
fruits.push("Orange"); | |
console.log(fruits); | |
console.log(fruits[0]); | |
fruits[1] = 'Banana' | |
console.log(fruits); | |
fruits.splice(0, 1) | |
console.log(fruits); | |
// --------------- Loops --------------- | |
const beatles = ["paul", "john", "ringo", "george"]; | |
beatles.forEach((beatle) => { | |
console.log(beatle.toUpperCase()); | |
}); | |
// --------------- Control flow --------------- | |
const age = 17; | |
if (age >= 18) { | |
console.log('You can vote'); | |
} else if (age === 17) { | |
console.log('You will be able to vote soon'); | |
} else { | |
console.log("You can't vote"); | |
} | |
const number = 12; | |
if (number !== 0) { | |
console.log('number is not zero') | |
} | |
const raining = true; | |
const accessory = raining ? "umbrella" : "sunglasses"; | |
// --------------- Objects --------------- | |
const student = { | |
firstName: 'John', | |
lastName: 'Doe' | |
}; | |
console.log(typeof student); | |
console.log(student); | |
console.log(student.firstName); | |
console.log(student['firstName']); | |
student.firstName = 'Jane'; | |
student['firstName'] = 'Jane'; | |
console.log(student); | |
const key = 'age' | |
student[key] = 40 | |
console.log(student); | |
// --------------- Functions --------------- | |
function oldSquare(x) { | |
return x * x; | |
} | |
const square = (x) => { | |
return x * x; | |
} | |
// const square = x => x * x; | |
console.log(oldSquare(5)); | |
console.log(square(5)); | |
// --------------- Function Example --------------- | |
const capitalize = (str) => { | |
// take the first letter | |
// uppercase it | |
const firstLetter = str[0].toUpperCase(); | |
// take the rest from the 2nd | |
// downcase it | |
const restOfTheWord = str.substring(1).toLowerCase(); | |
// create a new string = first letter + rest of the word | |
const result = `${firstLetter}${restOfTheWord}`; | |
// return it | |
return result; | |
} | |
console.log("Result = ", capitalize('waGoN')) // 'Wagon' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment