Javascript

Strings

You can also combine the character access with the .length property. So using the same language variable, here's how you get the second to last character from it:

const language = "JavaScript";
language.substring(1, 4); //"ava"

Template strings support interpolation! This means you could write a variable in your string, and get its value. The syntax is straightforward, you wrap your variable name with a dollar sign and curly braces. Let's take an example where we have a variable language with a value of JavaScript

length is a property that gives you the length of a string

.toUpperCase() is a function that converts the string to upper case

.toLowerCase() is a function that converts the string to lower case parentheses () on functions are required. .length is a property that is already pre-computed; therefore, it does not need parentheses.

console.log(...) is used for debugging and is NOT a replacement for return. Square brackets [index] are used to access a specific index from a string The index starts at 0. So the first character is index 0 You can combine it with the length of a string to get another character in another position A substring is a part or a portion of a string.

string.substring(indexStart, indexEnd) is used to return a portion of the string.

indexStart: the position of the first character you'd like to include.

indexEnd: the position of the first character you'd like to ignore. the indexEnd argument is optional which means you can leave it out. The + operator is used to add 2 numbers The + operator is used to concatenate 2 strings A template string is a string created with the backtick character: `

Conditions

Using an if condition, you can run a piece of code when the condition evaluates to true.

The syntax is if (condition) and then curly braces {} wrap the lines of code that correspond to this condition The else keyword can be used to perform some other code based on all the other conditions not satisfied with the if.

function canVote(age) {
if (age >= 18) {
return true;
}
return false;
}

Arrays

Arrays in JavaScript allow you to store multiple elements in the same variable. You can store numbers, strings, booleans, arrays, objects & more. These can be mixed within the same array. Here are some examples:

const users = []; // empty array
const grades = [10, 8, 13, 15]; // array of numbers
const attendees = ["Sam", "Alex"]; // array of strings
const values = [10, false, "John"]; // mixed

.length property

You can get the number of elements in an array by using the .length property. For example:

[].length; // 0
const grades = [10, 8, 13, 15];
grades.length; // 4

Get element by index

Similarly to strings, you can get an element from an array by using the square bracket syntax [] with the index starting from 0.

For example:

const users = ["Sam", "Alex", "Charley"];
users[1]; //"Alex"

You can also use the .at(index) method, which accepts negative indices making it easier to find the last element of the array. Here are some examples with .at():

const users = ["Sam", "Alex", "Charley"];
users.at(1); //"Alex"
users.at(-2); //"Alex"

Adding an element

You can add an element to an array by using the .push() method:

const numbers = [10, 8, 13, 15];
numbers.push(20); // returns 5 (the new length of the array)
console.log(numbers); // [10, 8, 13, 15, 20];