JavaScript: variables, datatypes and operators and loops
Tags: webdev, javascript, beginners, tutorial
Published: 2023-01-07 01:43:37

Hey guys today I am going to talk about my first learnings about JavaScript.

**values and variable**:

**_value_**: a value in JavaScript is nothing but a piece of data there are two types of values 1) The Primitive values 2) Non-primitive values

1) **_The primitive values or primitive datatype_**: this is where a single variable can store a single type of value . The different types of variables are :

- numbers(both integers and decimal numbers)

- strings which are collections of alphanumeric and symbols.

- Booleans (true or false)

- undefined data type means that the variable has been created, but has never been given a value.

- Null is similar to undefined, except it has to be set by coder intentionally 

**_ Non-primitive values or non-primitive data types_*: these are the data types which are made up of primitive data types (or primitive values). There are two types of non-primitives they are arrays and objects. 

**variables**: the are nothing but the place holder for the values. there are three types of variables 1)var 2)let 3)const.
the most used types of variables are let and const.

It is to be noted that const should be given a value immediately after using it for a variable.

**template literals** : Template literals (template strings) allow you to use strings or embedded expressions in the form of a string.
 
eg:

`console.log(`My Current First Name is ${firstname} ${lastname} ${mobileno}`);`

**operators**: the are special symbols to perform mathematical operations on the variables with data containing in them.

the major types of operators are shown in below :

- Arithmetic Operators:
As the name suggests, these operators perform arithmetic operations like addition(+), subtraction(-), multiplication(*), division(/), remainder(%), increment(++) and decrement(--).

- Assignment Operators:

Assignment operators(=) are binary operators that are used to assign values to variables.

- Comparison Operators
The comparison operators in JavaScript are binary operators. This means that it compares the two given operands and returns a Boolean value as the result. 

the comparison operators available in JavaScript are: 1)equal(==) 2) not equal(!=) 3) strict equal(===) 4)Strict not equal(!==) 5)Greater than(>) 6) Greater than or equal to(>=) 7) less than (<) 8) less than or equal to (<=)

**conditionals**: conditional statements are those statements that determine the flow of the program based on the pre-determined requirements that are required to be performed by the program. the different types of conditionals are :

1) if : this condition is used to check if the specified is true.

2) elseif: this condition is used to check if the next specified condition is true if the previous condition is false.

3) else: this is a condition where a all the above condition is false. 

eg: 
`if (false) {
      let outcome = "if block";
} else if (true) {
      let outcome = "else if block";
} else {
      let outcome = "else block";
}`

**switch case**: let us see the switch case with an example

`let expression = "expression-1"; 

switch (expression) {
    case "expression-1":
        console.log("expression-1");
        break;
    case "expression-2":
        console.log("expression-2");
        break;

    default:
        console.log("no expression-3")
        break;
}` 

as seen in the above example switch like if compares the expression with all the pre-determined cases and executes the block of code as per the required case.
 
note: when dealing with the conditionals especially with switch cases there are things such as breaking i.e. to break the program after meeting the conditions.

**loops**: it is a piece of code which performs a task sequentially until a certain condition is reached.

the most widely used loops in JavaScript are 1)for 2)while 3)do-while 

while loop is used when you don't know how many times does a loop need to run while for the for-loop it is opposite in case.

do-while is used when you want to run when the program needs to run at least once even if the condition is false

syntax of for loop 
`for ([initialExpression]; [conditionExpression]; [incrementExpression]){
  statement}` 

syntax of do-while loop:

`do {
    statement
} while (condition);`

syntax of while loop:

`while (condition){
  statement}`

there are other loops like for-of and for-each but these are built for arrays and objects.

the end and happy learning