javascriptreference sheet
javascriptreference sheet
press ctrl+f to search for something specific
this is a basic list, to view an advanced list, click here.
A variable is a container that holds a value. It's like a box that you can put something in, and then you can use the box to access that something later.
In JavaScript, you can create a variable using the var, let, or const keyword.
Here's an example of a variable being used:
let name = "John";
console.log(name); // Output: "John"
In this example, we created a variable called "name" and assigned it the value "John". We then use console.log to print the value of the variable.
Here's another example:
let age = 30;
age = 35;
console.log(age); // Output: 35
In this example, we created a variable called "age" and assigned it the value 30. Then we reassigned it to 35 and printed the value of the variable.
It's important to note that the const keyword is used to create a variable that cannot be reassigned.
const pi = 3.14;
pi = 3.15; // this will throw an error
In summary, variables are a way to store data in JavaScript and they can be created using var, let, or const keywords. You can then use the variable name to access the stored value.
Data types are the different types of values that a variable can hold. JavaScript has several built-in data types, including:
String (text, enclosed in quotes)
let name = "John";
Number (numeric value)
let age = 30;
Boolean (true or false)
let isStudent = true;
Null (no value)
let empty = null;
Undefined (a variable that has been declared but has no value)
let x;
console.log(x); // Output: undefined
Symbol (unique and immutable primitive value)
let symbol = Symbol("unique_identifier");
In JavaScript, the type of a variable is determined automatically based on the value it is assigned. For example, if you assign a string value to a variable, JavaScript will recognize it as a string type variable.
It's also important to note that JavaScript is a loosely typed language, which means that you don't need to specify the data type when you create a variable.
In summary, data types are the different types of values that a variable can hold in JavaScript, such as strings, numbers, booleans, null, undefined, and symbols. The type of a variable is determined automatically based on the value it is assigned.
An array is a collection of values. Think of it like a list of items. Each item in the list is called an element, and it can be of any data type, such as numbers, strings, objects, etc.
You can create an array in JavaScript by using square brackets [] and separating the elements with commas. Here's an example:
let fruits = ["Apple", "Banana", "Orange"];
console.log(fruits); // Output: ["Apple", "Banana", "Orange"]
In this example, we created an array called "fruits" that contains three elements: "Apple", "Banana", and "Orange".
You can also access individual elements of an array by using the index number of the element. The first element has an index of 0, the second element has an index of 1, and so on.
Here's an example of how to access elements of an array:
let fruits = ["Apple", "Banana", "Orange"];
console.log(fruits[0]); // Output: "Apple"
console.log(fruits[1]); // Output: "Banana"
console.log(fruits[2]); // Output: "Orange"
You can also use several built-in methods to manipulate arrays such as .push(), .pop(), .slice(), .splice(), .sort() and .reverse().
In summary, an array is a collection of values in JavaScript, and each value is called an element. You can create an array using square brackets [] and separate the elements with commas. You can access individual elements of an array by using their index number, and you can manipulate arrays using built-in methods.
JavaScript objects are used to store collections of related data. Think of it like a dictionary, where each item has a key-value pair. The key is a string that is used to identify the item, and the value can be of any data type, such as numbers, strings, arrays, other objects, etc.
You can create an object in JavaScript by using curly braces {} and separating the key-value pairs with commas. Here's an example:
let person = {
name: "John",
age: 30,
isStudent: true
};
console.log(person); // Output: {name: "John", age: 30, isStudent: true}
In this example, we created an object called "person" that contains three key-value pairs: {name: "John"}, {age: 30}, {isStudent: true}.
You can also access individual values of an object by using the key.
console.log(person.name); // Output: "John"
console.log(person.age); // Output: 30
console.log(person.isStudent); // Output: true
or
console.log(person["name"]); // Output: "John"
console.log(person["age"]); // Output: 30
console.log(person["isStudent"]); // Output: true
Objects can also have methods which are functions that are properties of an object.
let dog = {
bark: function(){
console.log('Woof Woof!');
}
}
dog.bark(); // Output: 'Woof Woof!'
In summary, JavaScript objects are used to store collections of related data in key-value pairs. You can create an object using curly braces {} and separate the key-value pairs with commas. You can access individual values of an object by using their key, and they can also have methods which are functions that are properties of an object.
JavaScript functions are blocks of code that can be executed when they are called. Functions allow you to group a set of instructions and reuse them multiple times, without having to write the same code again.
You can create a function in JavaScript using the function keyword. Here's an example:
function sayHello() {
console.log("Hello!");
}
In this example, we created a function called "sayHello" that contains one instruction: console.log("Hello!");. To execute the function, you simply call it by its name followed by parentheses () like this:
sayHello(); // Output: "Hello!"
Functions can also take input in the form of parameters and can also return output. Here's an example:
function add(a, b) {
return a + b;
}
let result = add(5, 10);
console.log(result); // Output: 15
In this example, we created a function called "add" that takes two parameters "a" and "b" and it returns the sum of them.
ES6 introduced arrow function expressions which are a shorthand for function expressions, they are more concise and easier to read. Here's an example:
let multiply = (a, b) => a * b;
console.log(multiply(5,10)); // Output: 50
In summary, JavaScript functions are blocks of code that can be executed when they are called, they allow you to group a set of instructions and reuse them multiple times. Functions can take input in the form of parameters and can also return output. ES6 introduced arrow function expressions which are a shorthand for function expressions and more concise.
JavaScript control flow refers to the way that the program's execution flows through different statements and blocks of code. It allows you to control which parts of your code get executed under certain conditions.
One of the most common ways to control the flow of code execution in JavaScript is through the use of if/else statements. The if statement checks if a condition is true, and if it is, the code inside the if block will be executed. Here's an example:
let age = 25;
if (age >= 18) {
console.log("You are an adult.");
}
In this example, the if statement checks if the value of age is greater than or equal to 18, and if it is, the code inside the block console.log("You are an adult.") will be executed.
You can also use else statements to specify what should happen if the condition is not true. Here's an example:
let age = 15;
if (age >= 18) {
console.log("You are an adult.");
} else {
console.log("You are not an adult.");
}
In this example, the code inside the else block console.log("You are not an adult.") will be executed because the condition is not true.
Another way to control the flow of code execution is by using switch statement which allows you to check different cases. Here's an example:
let day = 3;
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");
}
In this example, if day is 3 the output will be "Wednesday". If the value of day does not match any of the cases, the code inside the default block will be executed.
In summary, JavaScript control flow refers to the way that the program's execution flows through different statements and blocks of code. It allows you to control which parts of your code get executed under certain conditions using if/else statements and switch statements. These statements allow you to specify what should happen if a certain condition is true or not, or check different cases.
JavaScript loops are used to execute a block of code repeatedly until a certain condition is met. This allows you to iterate over elements in an array, for example, or perform an action multiple times.
There are several types of loops in JavaScript, but the most common are for loops, forEach loops, while loops, and do-while loops.
for loops are used to execute a block of code a certain number of times. Here's an example:
for (let i = 0; i < 5; i++) {
console.log("Hello!");
}
In this example, the loop will execute the code inside the block console.log("Hello!"); 5 times.
forEach loop is used to execute a function for each element in an array. Here's an example:
let fruits = ["Apple", "Banana", "Orange"];
fruits.forEach(function(fruit) {
console.log(fruit);
});
In this example, the loop will execute the code inside the block console.log(fruit); for each element in the array fruits.
while loops are used to execute a block of code as long as a certain condition is true. Here's an example:
let i = 0;
while (i < 5) {
console.log("Hello!");
i++;
}
In this example, the loop will execute the code inside the block console.log("Hello!"); as long as the condition i < 5 is true.
do-while loops are similar to while loops, but the block of code will be executed at least once before the condition is checked. Here's an example:
let i = 0;
do {
console.log("Hello!");
i++;
} while (i < 5);
In this example, the loop will execute the code inside the block console.log("Hello!"); at least once and as long as the condition i < 5 is true.
In summary, JavaScript loops are used to execute a block of code repeatedly until a certain condition is met. There are several types of loops in JavaScript such as for loops, forEach loops, while loops, and do-while loops. These loops allow you to iterate over elements in an array, perform an action multiple times, or execute a block of code as long as a certain condition is true.
ES6 (ECMAScript 6) is the latest version of JavaScript, which was released in 2015. It brings new features and improvements to the language, making it more powerful and easier to use.
One of the new features in ES6 is template literals, which allows you to include expressions inside string literals using backticks . Here's an example:
let name = "John";
console.log(`Hello ${name}!`); // Output: "Hello John!"
In this example, the ${name} inside the backticks is an expression that gets evaluated and its value is included in the string.
Another new feature in ES6 is destructuring, which allows you to extract values from objects and arrays and assign them to variables. Here's an example:
let person = {name: "John", age: 30};
let {name, age} = person;
console.log(name); // Output: "John"
console.log(age); // Output: 30
In this example, the variables name and age are destructured from the object person
ES6 also introduced arrow functions which are a shorthand for function expressions, they are more concise and easier to read. Here's an example:
let multiply = (a, b) => a * b;
console.log(multiply(5,10)); // Output: 50
In this example, we created an arrow function called "multiply" that takes 2 parameters "a" and "b" and it returns the product of them.
ES6 also introduced let and const keywords, which are used to declare variables. The let keyword is used to create variables that can be reassigned, while the const keyword is used to create variables that cannot be reassigned. Here's an example:
const pi = 3.14;
pi = 3.15; // this will throw an error
let x = 5;
x = 10; // this is allowed
In this example, we create a variable pi using the const keyword, which cannot be reassigned, and a variable x using the let keyword, which can be reassigned.
There are many other new features and improvements that come with ES6, such as classes, modules, and more. The use of these new features can make your code more readable, maintainable and efficient.
In summary, ES6 (ECMAScript 6) is the latest version of JavaScript which was released in 2015 and brings new features and improvements to the language, such as template literals, destructuring, arrow functions, and new ways to declare variables using let and const keywords, making it more powerful and easier to use.
everything below this line is incomplete. check back soon to see the rest!