JavaScript (Introduction & History of Js)

JavaScript (Introduction & History of Js)

From Mocha -> To JavaScript

The first thing that comes to your mind is JavaScript and java both are some (or) is JavaScript is evolved version of java, the answer to these questions is :

                          JavaScript and Java are both two different dimensions in the programming world.

Also, JavaScript is the most famous programming language in the world, some people may say python is the most famous language but the real thing is JavaScript is the most famous programing language. You can make a web app, mobile apps, desktop, and mobile games, and in the last couple of years machine learning with JavaScript and we came to know that JavaScript is used in blockchain also

Let Start With Fundamentals:

You know what is programming?

                                         Programming is when your program makes decisions based on conditions.

Basic of Programming Language are

  1. Value,
  2. Operation,
  3. Variable,
  4. Decision,
  5. Loop,
  6. Functions

History of JavaScript

In 1995 it was named Mocha in 1996 it was called LiveScript and after 2000 Netscape developed and named it JavaScript from till date it is called JavaScript.

   **Mocha (1995) -> LiveScript (1996) -> JavaScript(2000...till date)**

ECMA SCRIPT

In 2000 they were two famous browsers were Netscape browser and Internet Explorer. Netscape was one of the famous browsers at the time and they already have their Scripts. Inter Explorer also introduced their scripting language for them, which was client-said scripting as JS script. It was too difficult for a developer to code for two different browsers so they landed on ECMA ( It is an organization whose standards? are okay for all programming) so they raised to the ECMA from that time we call it as ECMA SCRIPT. *This was the small history of Js

Data Types:

                    Data Types are of two types one is primitive data types and non-primitive data types.

Primitive Data Type:

Anything having a single value is called Primitive Data Type.

INTEGER:

      7,
     1.6666

Both the whole number and decimal number are treated as integerINT

STRING:

"7"
"Hello Everyone"
' Hi All '

Anything inside the double and single quotes is called STRING

Boolean:

true; 1
false; 0

The name Boolean came from a famous Mathematician George Boole

Null & undefined

These data types are called empty values

Non-Primitive Data Types:

In JavaScript, we can store multiple collections of values in two ways in JavaScript. Non-Primitive data types are also called Object. Anything having multiple values stored together is called Non-Primitive Data Type.

  1. Array 2.

    Array:

    An array is nothing but a collection of values. These values are of different types in the array. An array can be written as [ ] . All the values in an Array are stored in a sequence means Index.
[ 24, 1.99,  "Hello World", true, ] 
index of 24                  is 0,
index of 1.99               is 1,
index of Hello World   is 2,
index of true               is 3

let arr = [ "value", 25, true]
console.log(arr) -> Output: ['value', 25, true]

let arr = ["value", 25, true]
console.log(arr[2]) -> Output: true

inside the array, each value has an index number the number starting from 0 in the array.

points to remember:

Syntax

The syntax is nothing but a set of rules.

Object:

In the case of Objects, they are stored in key-value pair format. Each value has its own key. The syntax for Object is

{
key: value
},
{abc: "My House", XYZ: "My Brother House"}

We all live in a house and all have our own keys to access our house like objects having keys for each value.

Variable:

x = 5

Here X is assigned by a value(5). To tell our computer we use the keyword (var, let, const). 5 is a value( To get this value in our system we assigned this value to a variable).

var x = 5  ( we didn't use Var in our day-to-day coding)

console.log(x) -> Syntax to display the output. node your filename -> Syntax to run Js in node

Keywords:

var, let, const

Only $ and _ characters are used in naming a variable, also variables always start with Alphabet.

Operations:

+, -, *, /, ==, ++, -- , ! ( These are some operators).
let value_1 = 25;
let value_2=  5;

console.log(value_1 + value_2) -> Output: 30

value_1 -> Operand 1.
value_2 -> Operand 2.

Concatenation:

Addition Operator (+) will do addition when both the operands are numbers, other than this will concatenate the two values.

let value_1 = 25;
let value_2= "5";

console.log(value_1 + value_2) -> Output: 255.
let userName = "John"
let mobileNumber = 9798730234

console.log("My username is " +userName+ "My mobile number is " + mobileNumber)
Output-> My username is JohnMy mobile number is 9798730234.

Interpolation (or) Template literal (or) Template strings:

In this case, we use a backtick symbol inside the console.log and for the variable name, we use the $ symbol to call a variable.

let userName = "John"
let mobileNumber = 9798730234

console.log(` My username: ${userName},
              My mobile no: ${mobileNumber}`).

Output: My username: John,
        My mobile no: 9798730234

Increment & Decrement Operator( ++ & -- ):

let abc = 5++
console.log(abc) -> This will throw error

Increment operator used always in a variable name

let abc = 5
let a     = abc++ (Post Increment)
console.log(a)  -> Output: 5

In this case of Pre Increment Operator

let abc = 5
let a     = ++ abc (pre Increment)
console.log(a)   -> Output: 6

Equal To Operator (==):

let rating = 5
if (rating == 5) {
console.log(" Excellent ")
} else {
console.log(" Four ")
}
This will print Excellent.

( == ) this operator checks the value only if we place "5" as a string instead of Int 5 it shows the same result in the loop. ( === ) this operator checks the value and the datatype.

let rating = "5"
if (rating ===5){
console.log(" Excellent ")
} else {
 console.log(" Four ")
}
This will print Four
let mail = false;
let mobile = true;

if (email == true && mobile == true) {
console.log("Both mail and mobile values are true")
} else if ( mobile == true || email == true) {
console.log("Any one condition is true")
}

[&& -> This  AND operator. It will run the if statement only if both conditions are true.
 ||    -> This OR operator will run else if statement only one condition is true.]

Ternary Operator:

Syntax: Condition? expression true: expression false.

var loggedIn = false;

var status = (loggedIn == true)? "You are in HomePage": " You Need to login"
console.log(status)

Switch Case:

Like if and else condition switch is also a condition statement which rarely used in our day-to-day coding life and in tech companies also.

Syntax:

 switch(expression) {
  case x:
    // code block
    break;
  case y:
    // code block
    break;
  default:
    // code block
}

Example for switch case:

Let's build user authentication using a switch case.

let user = " admin";

switch (user) {
 case "admin":
   console.log("You are logged as an admin")
   break;
case "student":
  console.log("You are logged as a student")
  break;
default:
  console.log("You are a trial user")
}

Here you send the user variable as a parameter to the switch case, if a user is admin it will print You are logged as an admin , if a user is student it will print You are logged as a student . In case the user is neither an admin nor a student the case statement will print the default.

Break in switch case:

The break is mandatory after each case in the switch but it's not needed for the default statement. In case you forget to write the break after case1, it will run all the cases in the switch including the default.