37
loading...
This website collects cookies to deliver better user experience
const daysOfTheWeek = 7;
let firstName = "Jane";
var lastName = "Doe";
function varExample ()
{
var name = "Jane";
if(name === "Jane")
{
var name = "Doe"; // the same variable as above
console.log(name) // will return "Doe"
}
console.log(name); // will also return "Doe"
}
function letExample ()
{
let name = "Jane";
if(name === "Jane")
{
let name = "Doe"; // new variable is created
console.log(name) // will return "Doe"
}
console.log(name); // will also return "Jane"
}
let firstName = "Jane";
let lastName = "Doe";
//joining them using a '+'
let fullNameOne = firstName + " " + lastName;
console.log(fullNameOne);
//or using template string by using backticks(``)
let fullNameTwo = `${firstName} ${lastName}`;
console.log(fullNameTwo);
//both will result in "Jane Doe"
let firstName = "Jane"
let firstNameLength = firstName.length
//will output "4"
console.log(firstNameLength);
let firstName = "Jane";
let upperCaseName = firstName.toUpperCase();
let lowerCaseName = firstName.toLowerCase();
console.log(upperCaseName);//Output: "JANE"
console.log(lowerCaseName);//Output: "jane"
//this will store all elements that have a class of "title" within our heading variable
let heading = document.getElementByClassName("title");
//this will store the element with the Id "submit" within our button variable
let button = document.getElementById("submit");
//this will store the *first* element that has a class of "title" within our heading variable
let heading = document.querySelector(".title");
//this will store the element with the Id "submit" within our button variable
let button = document.querySelector("#submit");
//this will store all of the elements that have a class of "title" within our heading variable as an array
let heading = document.querySelectorAll(".title");
//here we have our heading
const heading = document.querySelector('.heading');
//now let us add our subheading
const subheading = document.createElement('h5');
subheading.textContent = "I am a subheading";
//we can now add this subheading to the heading element
heading.appendChild(subheading);
<input type="text" class="username">
<button class="submit-btn" onClick="submitData()">
Submit
</button>
const submitData = () => {
let username = document.querySelector(".username").value;
console.log(username); //will print our the text entered in the input field to the console
}
<input type="text" class="username" onkeyup="logInput()">
<button class="submit-btn" onClick="submitData()">
Submit
</button>
const logInput = () => {
let username = document.querySelector(".username").value;
console.log(username);
//will produce if typed slow enough:
// "J"
// "Ja"
// "Jan"
// "Jane"
}
<input type="text" class="user-email" onkeyup="validateInput()">
const validateInput = () => {
//select the input element
let emailInputElement = document.querySelector(".user-email");
//get the value of the input field
let userEmail = emailInputElement.value;
//decide if the e-mail is valid or not
if(!userEmail.includes("@"))
{
//here we are adding the red border of the e-mail is valid
emailInputElement.classList.add("invalid-input");
//and removing it, if the e-mail becomes invalid again
emailInputElement.classList.remove("valid-input");
} else {
//here we add the green border if it is valid
emailInputElement.classList.add("valid-input");
//and remove the red border
emailInputElement.classList.remove("invalid-input");
}
}
//traditional JavaScript function
function generateFullName(firstName, lastName){
return `${firstName} ${lastName}`;
}
//will return "Jane Doe"
console.log(generateFullName("Jane", "Doe"));
//arrow function with name
const generateFullNameArrow = (firstName, lastName) => `${firstName} ${lastName}`
//arrow function returning "Jane Doe"
console.log(generateFullNameArrow("Jane", "Doe"));
let nameArray = ["Jane", "John", "Sarah", "Mike"];
nameArray.forEach(name => console.log(name));
// Output: "Jane"
// Output: "John"
// Output: "Sarah"
// Output: "Mike"
for(let i = 0; i < nameArray.length; i++)
{
console.log(nameArray[i]);
}
console.log(nameArray[i]);
is how we can access specific elements within an array.//index 0 1 2 3
let nameArray = ["Jane", "John", "Sarah", "Mike"];
//accessing the name Sarah
console.log(nameArray[2]);
//will give us a new array with names that have 4 letters or less
let namesThatHaveFourLetters = nameArray.filter(name => name.length <= 4);
//output: ["Jane", "John", "Mike"]
console.log(namesThatHaveFourLetters);
let randomNumbersArray = [1, 2, 3, 4, 5];
let doubledNumbersArray = randomNumbersArray.map(number => number * 2);
console.log(doubledNumbersArray);
//output: [2, 4, 6, 8, 10]
//add item to the end of an array
let nameArray.push("Amy");
//add item to the start of an array
let nameArray.unshift("Tom");
//remove item from the end of an array
let nameArray.pop(); //removes "Amy" from array
//remove item from the start of an array
let nameArray.shift(); // removes "Tom" from array
setTimeout( () => {
console.log("First console log");
}, 2000);
console.log("Second console log");
//Output:
//"Second console log"
//"First console log"
fetch("api/for/some/resource")
//Promises have a characteristic .then()
.then( response => {
console.log(response.data);
//it is common to use .catch() to log any errors
}).then( json => {
console.log(json);
}).catch( error => {
console.log(error);
});
//the async keyword comes before the function you want to use await in
const data = async () => {
//get the resource returned by the api
const resource = await fetch("api/for/some/resource")
//convert the returned data to json
const posts = await resource.json();
//make it available
return posts;
}
const getUserData = async () => {
const response = await fetch('api/user/resource', {
method: 'GET' //'POST', 'PUT', 'PATCH', 'DELETE'
});
const data = await response.json();
return data;
}
const formData = { firstName: "Jane" };
const postUserData = async () => {
const response = await fetch('api/user/create', {
method: 'POST',
headers: {
'Content-Type': 'application/json; charset=UTF-8'
},
body: JSON.stringify(formData),
});
const data = await response.json();
return data;
}
const library = require('library-name);
import library from 'library-name';
//roundNumber.js
export default decimal => Math.round(decimal);
//app.js
import roundNumber from './roundNumber.js';
let decimal = 3,2;
let roundedDecimal = roundNumber(decimal);
npm install
.npm install <package-name>
.npm install <package-name> --save
) and which are pure development dependencies (use npm install <package-name> --save-dev
). //package.json
{
"scripts": {
"dev": "vue-cli-service serve",
}
}
npm run dev
which will in this case start up the development server of our Vue app.