Wednesday, June 19, 2019

JavaScript

INTRO:- 
JavaScript is a very powerful client-side scripting language.
Javascipt is used mainly for enhancing the interaction of a user with the webpage.

A "HELLO WORLD" EXAMBLE 

  ➤ The on top of a section would possibly sound extremely exciting, and then it ought to — JavaScript is one among the foremost spirited net technologies, and as you begin to urge smart at exploitation it, your websites can enter a replacement dimension of power and power.

  ➤ However, changing into comfy with JavaScript could be very little tougher than changing into comfy with HTML and CSS. you will get to begin little and keep operating in little consistent steps. To start, we'll show the way to add some basic JavaScript to your page, making a "hello world!" example.

  1. First, attend your check web site and build a replacement folder named scripts. Then, inside the new scripts folder, you simply created, produce a replacement file referred to as main.js. put it aside in your scripts folder.

  2. Next, in your index.html file enter the subsequent component on a replacement line simply before the closing </body> tag:

<script src="scripts/main.js"></script> 

  3. This is essentially doing an equivalent job because the <link> component for CSS — it applies the JavaScript to the page, thus it will have a sway on the hypertext mark-up language (along with the CSS, and anything on the page).

  4. Now add the subsequent code to the most.js file:

var myHeading = document.querySelector('h1');

myHeading.textContent = 'Hello world!';

  5. Finally, confirm the hypertext mark-up language and JavaScript files square measure saved, then load index.html within the browser. you must see 'Hello World' in your browser.

now, 'Hello World!' will show on your browser. 


JavaScript Variable: Declare, Assign a value with Example 
  
  ➤ Variables area unit accustomed store values (name = "John") or expressions (sum = x + y).

  ➤ Declare Variables in JavaScript
Before employing a variable, you initially have to be compelled to declare it. you have got to use the keyword power unit to declare a variable like this:
  
  var name;    

  ➤ Assign a value to the Variable
You can assign a value to the variable either whereas declaring the variable or once declaring the variable.


var name = "John";
    
 OR

var name;

name = "John";

  ➤ Naming Variables
Though you'll be able to name the variables as you wish, it's honest programming follow to provide descriptive and significant names to the variables. Moreover, variable names ought, to begin with a letter and that they area unit case sensitive. thence the variables student name and studentName area unit totally {different|completely different} as a result of the letter n in an exceedingly name is different (n and N).

javaScript Array methods: create with Example

  ➤ What is associate Array?
An array is an associated object that may store a set of things. Arrays become very helpful once you got to store massive amounts of information of the constant kind. Suppose you would like to store details of five hundred workers. If you're victimization variables, you'll have to be compelled to produce five hundred variables whereas you'll do constantly with one array. you'll access the things in the associate array by pertaining to its index number and therefore the index of the primary part of the associate array is zero.

  ➤ JavaScript create Array
You can create an associate array in JavaScript as given below.

 var students = ["John", "Ann", "Kevin"];

  ➤ Here, you're initializing your array as and once it's created with values “John”, “Ann” and “Kevin”.The index of “John”, “Ann” and “Kevin” is zero, one and a pair of severally. If you would like to feature a lot of parts to the scholars array, you'll pair like this:

 students[3] = "Emma";

 students[4] = "Rose";

  ➤ You can conjointly create associate array victimization Array builder like this:


var students = new Array("John", "Ann", "Kevin");

      OR

var students = new Array();

students[0] = "John";

students[1] = "Ann";

students[2] = "Kevin";

  ➤ JavaScript Array Methods
The Array object has several properties and ways that facilitate developers to handle arrays simply and expeditiously. you'll get the worth of a property by specifying array name.property and therefore the output of a way by specifying array name.method().

  1. length property --> If you would like to understand the number of parts in the associate array, you'll use the length property.

  2. prototype property --> If you would like to feature new properties and ways, you'll use the example property.

  3. reverse technique --> you'll reverse the order of things in associate array employing a reverse technique.
  
  4. sort technique --> you'll type the things in associate array victimization type technique.

  5. pop technique --> you'll take away the last item of associate array employing a pop technique.
  6. shift technique --> you'll take away the primary item of associate array victimization shift technique.

  7. push technique --> you'll add a price because of the last item of the array.

For, while and Do while LOOP in JavaScript (with Example) 

  ➤ How to use Loop?
Loops are helpful once you need to execute equivalent lines of code repeatedly, for a selected variety of times or as long as a selected condition is true. Suppose you wish to blood group ‘Hello’ message one hundred times in your webpage. Of course, you'll need to copy and paste an equivalent line one hundred times. Instead, if you employ loops, you'll be able to complete this task in exactly three or four lines.

  ➤ Different Types of Loops
There are in the main four varieties of loops in JavaScript.

   1. for loop
   2. for/in a loop (explained later)
   3. while loop
   4. do…while loop

for loop 

Syntax:


for(statement1; statement2; statment3)

      {

         executed

             }
  ➤ The statement1 is executed initial even before corporal punishment the iteration code. So, this statement is often accustomed to assign values to variables which will be used within the loop.
The statement2 is the condition to execute the loop.

The statement3 is executed when the iteration code is executed.

while loop


Syntax:


while(condition)

      {

         executed

        }


 ➤ The “while loop” is executed as long because the fixed condition is true. within the while loop, you must embody the statement which will finish the loop at some purpose of your time. Otherwise, your loop can ne'er finish and your browser might crash.

do…while loop

Syntax:


    do

      {

        block of code to be executed

           } while (condition)


  ➤ The do…while loop is extremely kind of like a while loop. the sole distinction is that in do…while loop, the block of code gets executed once even before checking the condition.



No comments:

Post a Comment

Search This Blog