Shiva Bhusal
Shiva's Blog

Follow

Shiva's Blog

Follow
Part 2  - Javascript For Beginners

Photo by Markus Spiske on Unsplash

Part 2 - Javascript For Beginners

Shiva Bhusal's photo
Shiva Bhusal
·Jan 6, 2021·

3 min read

Play this article

Introduction

This webpage is a continuation of a beginner's guide to JavaScript and covers topics such as data types, conditional statements, loops, and functions.

What are Basic operators?

Basic operators are addition +, multiplication *, subtraction -, Division /, Remainder % , Exponentiation **.and so on. For example:


alert( 5 % 2 ); // 1, a remainder of 5 divided by 2 //Remainder operator

alert( 2 ** 2 ); // 4  (2 multiplied by itself 2 times) //Exponentiation operator

What is String concatenation? String concatenation means merging/connecting two strings with the plus operator '+'.

For Example:


let str = "my" + "string";//alert(str); // mystring

alert( '1' + 2 ); // "12"
alert( 2 + '1' ); // "21"

alert(2 + 2 + '1' ); // "41" here firstly two numbers are added then 4 is converted to string then, adds string '4' to string '1'.

From this above example, we can understand that string '1' tries to convert number 2 to string and adds 12 whereas "21" is also the same 2 is converted at the string and added with string '1' and so on.

Rules of '+=' and '*=' operators:


let n = 2;
n += 5; // n = 7 operator + is calculated first like 2+5=7.
n *= 2; // n = 14 now value of 'n' is 7 so 7*2=14.

alert( n ); // 14

How are Increment/decrement operations done?

Increment++ increases a variable by '1'. Decrement-- decreases a variable by '1'.

For Example:


let counter = 2;
counter++;        // 2+1=3 increment by '1'.
alert( counter ); // returns 3


let counter = 2;
counter--;        //2-1=1 decrement by '1'.
alert( counter ); // 1


let a = 1, b = 1;
alert( ++a ); // 2, prefix form returns the new value
alert( b++ ); // 1, postfix form returns the old value

alert( a ); // 2, incremented once
alert( b ); // 2, incremented once

What are comparison operators?

Comparison operators are those operators which are used for comparing two operands.


1. a > b, a < b // greater than less then operators.
2. a >= b, a <= b // greater/less then equal to.
3. a == b // eqality test.
4. a = b // assigning value 'a' to value 'b'.
5. a != b // 'a' is not equal to 'b'.
6. a === b //strict operator


Simple Example:
alert( 2 > 1 );  // true 
alert( 2 == 1 ); // false 
alert( 2 != 1 ); // true

What is Conditional branching?

conditional branching is the term used to describe operations such as 'if(condition)', 'else(condition)', 'else if', and 'if else' by using different operators.

For Example:


let year = prompt('In which year was the ECMAScript-2015 specification published?', '');

if (year < 2015) {
  alert( 'Too early...' );
} else if (year > 2015) {
  alert( 'Too late' );
} else {
  alert( 'Exactly!' );
}

In the code above, JavaScript first checks year < 2015. If that is false, it goes to the next condition year > 2015. If that is also false, it shows the last alert. There can be more else if blocks. The final else is optional.

Note: A number 0, an empty string `""` , null, undefined, and NaN all become false. Because of that, they are called “falsy” values. Other values become true, so they are called “truthy”.

What are logical operators?

Logical operators are '|| (OR), && (AND), ! (NOT)'.

 
Share this