Web Dev Streaks Day- 22 (Milestone 4: Basic Javascript)
Module 17: Fundamentals Concepts (Array & Conditionals)
1. Declare Array, Array Length & Array Index
If you have to store multiple data in one variable then you will need an Array for that.
To declare an Array, just enclose the data within square braces “[ ]” and separate them using commas “,”
you also can see an array length using arrayName.length. And the position of array elements starts from zero (0) which is called array index.
2. Array Index, get and set by index, indexOf
You can get the position of any array element using the “indexOf” function.
If you try to find an element that isn’t in the array the indexOf function returns position value = “-1” which isn’t really possible which means that element doesn’t belong to that array.
You can access any element by its position (array[position]). You also can set that value to any variable using the assignment operator (=).
Similarly, If you can change any existing array element, then access that element by its position and then assign that value you want.
3. Add or Remove Element from Array Using push, pop
If you have to add a new element into the array then you can push that element into the array.
array.push(“newElement”);
Now, this “newElement” will be inserted into the Array. Remember the newly pushed element always will be inserted at the last.
In the same way, if you have to remove any element from a
n array then you have to do the pop operation. To pop an element from an array just do,
array.pop();
Remember, you can pop elements till the array get empty.
One more thing, you can add elements and also pop elements from the front in javascript. Just think about it for a while and if you can’t find any solution just simply use Google.
4. Compare Variables & Comparison Operator
A comparison operator compares its operands and returns a logical value based on whether the comparison is true. The operands can be numerical, string, logical, or object values. Strings are compared based on standard lexicographical ordering, using Unicode values. In most cases, if the two operands are not of the same type, JavaScript attempts to convert them to an appropriate type for the comparison. This behavior generally results in comparing the operands numerically. The sole exceptions to type conversion within comparisons involve the === and !== operators, which perform strict equality and inequality comparisons. These operators do not attempt to convert the operands to compatible types before checking equality.
Logical operators are typically used with Boolean (logical) values; they return a Boolean value when they are. However, the
&&
and||
operators actually return the value of one of the specified operands, so if these operators are used with non-Boolean values, they may return a non-Boolean value.
Here, are some examples which have been done for you,
5. Make Conditional Decision, if-else, Comparison
A conditional statement is a set of commands that executes if a specified condition is true. JavaScript supports two conditional statements: “if…else” and “switch”.
if….else statement
Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false.
Here, the condition can be any expression that evaluates to true or false. (See Boolean for an explanation of what evaluates to true and false)
If the condition evaluates to true, statement_1 is executed. Otherwise, statement_2 is executed. statement_1 and statement_2 can be any statement, including further nested if statements.
6. Handle Multiple Conditions, and, or
Sometimes there will be needed to test multiple conditions within the if statement. In that case, you can write them up using parenthesis inside the if statement.
Likewise, follow the examples down below,
7. (Advanced) Multi-Stage Condition & Nested Conditions
You can also compound the statements using else if to have multiple conditions tested in sequence, as follows:
In the case of multiple conditions, only the first logical condition evaluates to true will be executed. To execute multiple statements, group them within a block statement ({…}).
A nested if statement is an if-else statement with another if statement as the if the body or the else body.
Now take a look at the example that has been done for you and try to understand each line.
Also, as you’re new in this field you won’t be able to catch these things up until you practice more and more.