Basics of Javascript
Conditions
The if statement
Use the if statement to specify a block of JavaScript code to be executed if a condition is true.
The else Statement
Use the else statement to specify a block of code to be executed if the condition is false.
The else if Statement
Use the else if statement to specify a new condition if the first condition is false.
Loops
The For Loop
The for loop is often the tool you will use when you want to create a loop.
The for loop has the following syntax:
for (statement 1; statement 2; statement 3) {
code block to be executed }
Statement 1 is executed before the loop (the code block) starts.
Statement 2 defines the condition for running the loop (the code block).
Statement 3 is executed each time after the loop (the code block) has been executed.
The While Loop
The while loop loops through a block of code as long as a specified condition is true.
The Do/While Loop
- The do/while loop is a variant of the while loop.
- This loop will execute the code block once, before checking if the condition is true, then it will repeat the loop as long as the condition is true.
String Manipulations
indexOf()
The indexOf() method returns the index of (the position of) the first occurrence of a specified text in a string:
search()
The search() method searches a string for a specified value and returns the position of the match:
slice()
- slice() extracts a part of a string and returns the extracted part in a new string.
- The method takes 2 parameters: the starting index (position), and the ending index (position).
substring()
- substring() is similar to slice().
- The difference is that substring() cannot accept negative indexes.
substr()
- substr() is similar to slice().
- The difference is that the second parameter specifies the length of the extracted part.
replace()The replace() method replaces a specified value with another value in a string:
No comments:
Post a Comment