Master modern Javascript (ES6+) to build dynamic automation and web-based tools.
Learn how to store data in Javascript using let and const.
// Variables and Data Types
let name = 'John'; // String
const age = 30; // Number
let isCool = true; // Boolean
let x = null; // Null
let y = undefined; // Undefined
let person = { // Object
firstName: 'John',
lastName: 'Doe'
};
let colors = ['red', 'blue']; // Array
console.log(typeof name);
console.log(name, age, isCool);
"Hello" and print it.
Learn about string manipulation and template literals.
// Strings
const name = 'John';
const age = 30;
// Concatenation
console.log('My name is ' + name + ' and I am ' + age);
// Template Literals
console.log(`My name is ${name} and I am ${age}`);
// String Methods
const s = 'Hello World';
console.log(s.length);
console.log(s.toUpperCase());
console.log(s.toLowerCase());
console.log(s.substring(0, 5));
console.log(s.split(''));
Arrays are used to store multiple values in a single variable.
// Arrays
const numbers = [1, 2, 3, 4, 5];
const fruits = ['apples', 'oranges', 'pears'];
console.log(numbers);
console.log(fruits[1]);
// Add to end
fruits.push('mangos');
// Add to beginning
fruits.unshift('strawberries');
// Remove from end
fruits.pop();
// Check if array
console.log(Array.isArray(fruits));
// Get index
console.log(fruits.indexOf('oranges'));
Object literals are key-value pairs representing data objects.
// Object Literals
const person = {
firstName: 'John',
lastName: 'Doe',
age: 30,
hobbies: ['music', 'movies', 'sports'],
address: {
street: '50 main st',
city: 'Boston',
state: 'MA'
}
};
console.log(person.firstName, person.lastName);
console.log(person.hobbies[1]);
console.log(person.address.city);
// De-structuring
const { firstName, lastName, address: { city } } = person;
console.log(city);
// Add property
person.email = 'john@gmail.com';
Learn about named and arrow functions in Javascript.
// Functions
function greet(greeting = 'Hello', name = 'John') {
return `${greeting} ${name}`;
}
console.log(greet('Hi', 'Asad'));
// Arrow Functions
const addNums = (num1 = 1, num2 = 1) => num1 + num2;
console.log(addNums(5, 5));
const multiplyByTwo = num => num * 2;
console.log(multiplyByTwo(10));