TECHNICALSTUDY

The associate of technical study article for computer science students which could gain the knowledge about to technicals like Computer history, hard disk, printer,central processing unit etc. Technicalstudy also considered software engineering, operating system and some chunks of programming languages.

Email subscription

Enter your email address:

Delivered by FeedBurner

Sunday, September 17, 2023

 ===========================JAVASCRIPT=======================

INTRODUCTION :

JavaScript (js) is a light-weight object-oriented programming language which is used by several

websites for scripting the webpages. It is an interpreted, full-fledged programming language that

enables dynamic interactivity on websites when applied to an HTML document. It was introduced

in the year 1995 for adding programs to the webpages in the Netscape Navigator browser.

1- What is JavaScript in explain?

What is JavaScript ? JavaScript is a dynamic computer programming language. It is lightweight and

most commonly used as a part of web pages, whose implementations allow client-side script to

interact with the user and make dynamic pages. It is an interpreted programming language with

object-oriented capabilities.

JavaScript is an asynchronous and concurrent programming language that offers a lot of flexibility.

  // step 1: "use strict" : it used for some restriction to write relevant code
without error;
    "use strict";

2- JAVASCRIPT STATEMENT :-

JavaScript statements are composed of:

Values, Operators, Expressions, Keywords, and Comments.

Example: document.getElementById("demo").innerHTML = "Hello Dolly.";

3- The JavaScript syntax defines two types of values:

Fixed values

Variable values

Fixed values are called Literals.

Variable values are called Variables.

Literals :

3.1. Numbers are written with or without decimals:

Example- 10.50

1001

3.2. Strings are text, written within double or single quotes:

Example- "John Doe"

'John Doe'

Variables :

Using var - We can redeclare and reassign.

var x= 30;

var x= 20;

Using let - We can not redeclare but reassign.

let x =30;

x=20;

Using const - We can not redeclare and reassign.

const = 30;

variables are used to store data values, it's case sensitive.

A JavaScript variable declaration’s name must begin with:

A letter (A-Z or a-z)

A dollar sign ($)

Or an underscore (_)

Historically, programmers have used different ways of joining multiple words into one variable name:

Hyphens:

first-name, last-name, master-card, inter-city.

Note:-Hyphens are not allowed in JavaScript. They are reserved for subtractions.


Underscore:

first_name, last_name, master_card, inter_city.

Upper Camel Case (Pascal Case):

FirstName, LastName, MasterCard, InterCity.

Lower Camel Case:

JavaScript programmers tend to use camel case that starts with a lowercase letter:

firstName, lastName, masterCard, interCity.

4-Comment :

Conment is not executable statement in programming language there are

two type of programming language.

4.1 - Single line comment start with // -

// Not Excecutable statment

4.2 – Multiple line comment start with /* code... */-

/* Not Excecutable statment*/

5 – What is Tokens : Smallest unit/ Element of program call token.

Like : c = a + b;

There are a,=,a, +,b is smallest element of program.

5.1 – Constant : Constants are those who value never can be changes.

5.2 – Keyword : Keyword are those word which meaning already defined in compilers. Is is also called

RESERVE WORD.

Like : strings, numbers, booleans, undefined, and null.

5.3 – Identifier : It will identify variable, datatype, class,interface,function.

Like : let a; var c = a-b, const a1;

5.4 – Datatype : Datatype are those which is define storage for memory allocation.

JavaScript has 8 Datatypes

1. String

2. Number

3. Bigint

4. Boolean

5. Undefined

6. Null

7. Symbol

8. Object

The Object Datatype

The object data type can contain:

1. An object

2. An array

3. A date

5.5 – Operators : Operator are those which is perform the operation between operant.

Like: The Addition Operator + adds numbers:

The Assignment Operator = assigns a value to a variable.

Let X =6

Let Y =4

X +Y= Z

The Assignment Operator (=) assigns a value to a variable:

Let Z = 10;


Types of JavaScript Operators

Arithmetic Operators :

Example :

let a = 3;

let x = (100 + 50) * a;

Assignment Operators :

The Addition Assignment Operator (+=) adds a value to a variable.

Example

x= 10;

x += 5;

Comparison Operators :

Example :

let a1 =11;

let b2 =14;

let result = a< b;

String Operators :

Example:

let text1 = "A";

let text2 = "B";

let result = text1 < text2;

The + can also be used to add (concatenate) strings:

let text1 = "John";

let text2 = "Doe";

let text3 = text1 + " " + text2;

Logical Operators :

A typical arithmetic operation operates on two numbers.there are +,-,*,/,%,increments,decrements;

The two numbers can be literals:

let x = 100 + 50;

Incrementing

The increment operator (++) increments numbers. & The Decrement operator (--) increments numbers

let x = 5;

x++; , x--;

let z = x;

Bitwise Operators :

Ternary Operators :

Like : a>b ? True : False

6- Conditional Statements :

Very often when you write code, you want to perform different actions for different decisions.

You can use conditional statements in your code to do this.

In JavaScript we have the following conditional statements:

Use if to specify a block of code to be executed, if a specified condition is true

Use else to specify a block of code to be executed, if the same condition is false

Use else if to specify a new condition to test, if the first condition is false

Use switch to specify many alternative blocks of code to be executed

6.1- if (condition) {

// block of code to be executed if the condition is true

}


6.2- if (condition) {

// block of code to be executed if the condition is true

} else {

// block of code to be executed if the condition is false

}

6.3- if (condition1) {

// block of code to be executed if condition1 is true

} else if (condition2) {

// block of code to be executed if the condition1 is false and condition2 is true

} else {

// block of code to be executed if the condition1 is false and condition2 is false

}

6.4-switch(expression) {

case x:

// code block

break;

case y:

// code block

break;

default:

// code block

}

7- Loop statements : loop statements are specify the range of program.

JavaScript supports different kinds of loops:

for - loops through a block of code a number of times

foreach -

while - loops through a block of code while a specified condition is true

do/while - also loops through a block of code while a specified condition is true

The for statement creates a loop with 3 optional expressions:

for (initialization 1; condition 2 ; increment/decrement 4) {

// 3 Execute block of code

}

for (let i =0; i <5; i++) {

text += "The number is " + i +"<br>";

}

The while loop is use to controll loop. This statement firstly check the condition given with while(). If

condition is true will execute block of code.

while (i < 10) {

text += "The number is " + i;

i++;

}

The Do while loop is also use to controll loop. This statement will be execute unconditionaly once and then

codition will check.

let i = 0;

do {

text += "The number is " + i;

i++;

}

while (i < 10);

The forEach method calls a function for each element in an array.

The forEach() method is not executed for empty elements.

<script>

let text = "";

const fruits = ["apple", "orange", "cherry"];

fruits.forEach(myFunction);

function myFunction(item, index) {

text += index + ": " + item + "<br>";

}

</script>

7.1- What is Object Declaration and Definition:

 let obj = {
         b:9,
         c:"shubh",
         func : function hello(number){
            console.log("THis is Object's function assigned calling +"+ number);
         }
     }


7.2- Array print with For loop and ForEach loop:

 console.log("Using For loop");
 
let array = [2,41,10,"shubham",'char',12.01];

 for(let i =0; i<array.length; i++){
   console.log(array[i]);
 }


7.3- Array print with ForEach loop:

 
console.log("Using ForEach loop");

 let array = [2,41,10,"shubham",'char',12.01];

 array.forEach(element =>{
    console.log(element)
 });


8- What is Arrow Function:

Arrow functions are us to write shorter function syntax:

Before Arrow:

hello = function() {

return "Hello World!";

}

let normalfunct = function() =>{
    console.log("The number is normal function.");
  }
  normalfunct();


With Arrow Function:

hello = () => {

return "Hello World!";

}

// arrow function using
  let hello = (num) =>{
    console.log("The number is "+this);
  }
  hello();


JavaScript Asynchronous

9- What is Callback Function:

A Callback function is function that is passed an argument to another function.

// function

function greet(name, callback) {

console.log('Hi' + ' ' + name);

callback();

}

// callback function

function callMe() {

console.log('I am callback function');

}

// passing function as an argument

greet('Peter', callMe);

Note : callMe function belong to key callback parameter.


10- JavaScript Promise and Promise Chaining:

In JavaScript, a promise is a good way to handle asynchronous operations. It is used to find out if the

asynchronous operation is successfully completed or not.

A promise may have one of three states.

i- Pending

ii- Fulfilled

iii- Rejected

A promise starts in a pending state. That means the process is not complete. If the operation is successful, the

process ends in a fulfilled state. And, if an error occurs, the process ends in a rejected state.

For example, when you request data from the server by using a promise, it will be in a pending state. When

the data arrives successfully, it will be in a fulfilled state. If an error occurs, then it will be in a rejected state.

Create a Promise

To create a promise object, we use the Promise() constructor.

let promise = new Promise(function(resolve, reject){

//do something

});

The Promise() constructor takes a function as an argument. The function also accepts two functions resolve()

and reject().

If the promise returns successfully, the resolve() function is called. And, if an error occurs, the reject()

function is called.

Example 1: Program with a Promise

const count = true;

let countValue = new Promise(function (resolve, reject) {

if (count) {

resolve("There is a count value.");

} else {

reject("There is no count value");

}

});

console.log(countValue);

11- Javscript async/await :

In this tutorial, you will learn about JavaScript async/await keywords with the help of examples.

We use the async keyword with a function to represent that the function is an asynchronous function. The

async function returns a promise.

The syntax of async function is:

async function name(parameter1, parameter2, ...paramaterN) {

// statements

}

Here,

name - name of the function

parameters - parameters that are passed to the function

JavaScript await Keyword -

The await keyword is used inside the async function to wait for the asynchronous operation.

let result = await promise;

The use of await pauses the async function until the promise returns a result (resolve or reject) value. For

example,


=============Quick Revise Practise Code :===========

<script>

        // stp 1: "use strict" : it used for some restriction to write relevent code without error;
       "use strict";
        let a = 44;
        console.log(a);
       
        /* DataType is following :*/
        //  numbers
        //  String
        //  Boolean
        //  undefined
        //  null
         
    // step 2:  object declaration  
     let obj = {
         b:9,
         c:"shubh",
         func : function hello(number){
            console.log("THis is Object's function assigned calling +"+ number);
         }
     }

// step 3:  event in js

document.addEventListener("click",function(){
    alert("Hello shubham");
    let conf = confirm("are ok with it ?");
    console.log(conf);
});

// step 4 : Array in js
 let array = [2,41,10,"shubham",'char',12.01];
 console.log("Using For loop");
 for(let i =0; i<array.length; i++){
   console.log(array[i]);
 }

 console.log("Using ForEach loop");
 array.forEach(element =>{
    console.log(element)
 });


// step 5 : arrow function using use restric keyword
  let arrowfunct = (num) =>{
    console.log("The number is "+this);
  }
  arrowfunct();

// step 5 : asynchronous operations
  setTimeout(() => {
    console.log("hello setTimeout: perform asynchronize operation callback
function call after few second.");
  }, 3000);
console.log("just about to below content.");
</script>



No comments:

Post a Comment

Adbox