What is JavaScript | Introduction | Data types | Full explanation of JavaScript

JS Documentation

  • Introduction
  • What you should already know
  • JavaScript and Java
  • Hello world
  • Variables
  • Declaring variables
  • Variable scope
  • Global variables
  • Constants
  • Data types
  • if...else statement
  • while statement
  • Function declarations
  • Reference





Introduction

JavaScript is a cross-stage, object-arranged scripting language. It is a little and lightweight language. Inside a host situation (for instance, an internet browser), JavaScript can be associated with the objects of its condition to give automatic authority over them. 

JavaScript contains a standard library of items, for example, Array, Date, and Math, and a center arrangement of language components, for example, administrators, control structures, and proclamations. Center JavaScript can be stretched out for an assortment of purposes by enhancing it with extra articles; for instance:


Customer side JavaScript expands the center language by providing items to control a program and its Document Object Model (DOM). For instance, customer side expansions enable an application to put components on a HTML structure and react to client occasions, for example, mouse clicks, structure information, and page route.

Server-side JavaScript broadens the center language by providing objects pertinent to running JavaScript on a server. For instance, server-side augmentations enable an application to speak with a database, give coherence of data starting with one summon then onto the next of the application, or perform record controls on a server.

What you should already know

This guide assumes you have the following basic background:

A general understanding of the Internet and the World Wide Web (WWW).
Good working knowledge of HyperText Markup Language (HTML).

Some programming experience. If you are new to programming, try one of the tutorials linked on the main page about JavaScript.

JavaScript and Java

JavaScript and Java are comparative somehow or another yet in a general sense diverse in some others. The JavaScript language takes after Java however does not have Java's static composing and solid sort checking. JavaScript pursues most Java articulation language structure, naming shows and fundamental control-stream develops which was the motivation behind why it was renamed from LiveScript to JavaScript. 

As opposed to Java's gather time arrangement of classes worked by statements, JavaScript bolsters a runtime framework dependent on few information types speaking to numeric, Boolean, and string esteems. JavaScript has a model based item model rather than the more typical class-based article model. The model based model gives dynamic legacy; that is, what is acquired can fluctuate for individual articles. JavaScript likewise supports capacities with no exceptional definitive prerequisites. Capacities can be properties of items, executing as approximately composed techniques. 

JavaScript is an exceptionally freestyle language contrasted with Java. You don't need to announce all factors, classes, and strategies. You don't need to be worried about whether techniques are open, private, or ensured, and you don't need to actualize interfaces. Factors, parameters, and capacity return types are not expressly composed.

Hello world

To get started with writing JavaScript, open the Scratchpad and write your first "Hello world" JavaScript code:
 
function greetMe(yourName)  alert("Hello " + yourName); greetMe("World"); 

Select the code in the pad and hit Ctrl+R to watch it unfold in your browser!

Variables

You use factors as emblematic names for qualities in your application. The names of factors, called identifiers, fit in with specific principles. 

A JavaScript identifier must begin with a letter, underscore (_), or dollar sign ($); consequent characters can likewise be digits (0-9). Since JavaScript is case touchy, letters incorporate the characters "A" through "Z" (capitalized) and the characters "a" through "z" (lowercase). 

You can utilize ISO 8859-1 or Unicode letters, for example, Ã¥ and ü in identifiers. You can likewise utilize the Unicode break groupings as characters in identifiers. A few instances of legitimate names are Number_hits, temp99, and _name. 


Declaring variables

You can pronounce a variable in three different ways: With the catchphrase var. For instance, var x = 42. This linguistic structure can be utilized to announce both nearby and worldwide factors. 

By essentially appointing it a worth. For instance, x = 42. This consistently announces a worldwide variable. It produces an exacting JavaScript cautioning. You shouldn't utilize this variation. 

With the watchword let. For instance, let y = 13. This linguistic structure can be utilized to announce a square extension nearby factor. See Variable degree beneath.

Variable scope

When you announce a variable outside of any capacity, it is known as a worldwide variable, since it is accessible to some other code in the present record. When you proclaim a variable inside a capacity, it is known as a neighborhood variable, since it is accessible just inside that work. 

JavaScript before ECMAScript 2015 does not have square articulation scope; rather, a variable announced inside a square is nearby to the capacity (or worldwide extension) that the square dwells inside. For instance the accompanying code will log 5, in light of the fact that the extent of x is the capacity (or worldwide setting) inside which x is proclaimed, not the square, which for this situation is an if explanation.

if (true) 
{ var x = 5; } console.log(x); // 5
{ let y = 5; } 
console.log(y); // ReferenceError: y is not defined

This behavior changes, when using the let declaration introduced in ECMAScript 2015.

Global variables

Worldwide factors are in actuality properties of the worldwide item. In website pages the worldwide item is window, so you can set and access worldwide factors utilizing the window.variable sentence structure. 

Thus, you can get to worldwide factors proclaimed in one window or edge from another window or casing by determining the window or edge name. For instance, if a variable called phoneNumber is pronounced in a record, you can allude to this variable from an iframe as parent.phoneNumber.

Constants

You can make a read-just, named steady with the const watchword. The linguistic structure of a steady identifier is equivalent to for a variable identifier: it must begin with a letter, underscore or dollar sign and can contain alphabetic, numeric, or underscore characters. 

const PI = 3.14; A consistent can't change an incentive through task or be re-announced while the content is running. It must be instated to a worth. 

The extension rules for constants are equivalent to those for let square degree factors. In the event that the const catchphrase is excluded, the identifier is accepted to speak to a variable. 

You can't announce a steady with a similar name as a capacity or variable in a similar extension. For instance:


// THIS WILL CAUSE AN ERROR 

function f() {}; const f = 5; // THIS WILL CAUSE AN ERROR ALSO function f() { const g = 5;  var g; //statements }

However, object attributes are not protected, so the following statement is executed without problems.
 
const MY_OBJECT = {"key": "value"}; MY_OBJECT.key = "otherValue";

Data types

The latest ECMAScript standard defines seven data types:
Six data types that are primitives:

Boolean. 
true and false.

null. 
A special keyword denoting a null value. Because JavaScript is case-sensitive, null is not the same as Null, NULL, or any other variant.

undefined.
A top-level property whose value is undefined.

Number. 
42 or 3.14159.

String. 
"Howdy" Symbol (new in ECMAScript 2015). A data type whose instances are unique and immutable.

Object.In spite of the fact that these information types are a moderately limited quantity, they empower you to perform valuable capacities with your applications. Articles and capacities are the other major components in the language. You can consider articles named compartments for qualities, and capacities as techniques that your application can perform.

if...else statement

Use the if statement to execute a statement if a logical condition is true. Use the optional else clause to execute a statement if the condition is false. An if statement looks as follows: 
 if (condition) 
{ statement_1; } else { statement_2; } 

condition can be any articulation that assesses to genuine or false. See Boolean for a clarification of what assesses to genuine and false. On the off chance that condition assesses to genuine, statement_1 is executed; something else, statement_2 is executed. statement_1 and statement_2 can be any announcement, including additionally settled if explanations. You may likewise exacerbate the announcements utilizing else if to have various conditions tried in succession, as pursues:

if (condition_1) 
{ statement_1; } 
else if (condition_2) { statement_2; } else if (condition_n) { statement_n; } else { statement_last; } 


On account of various conditions just the principal coherent condition which assesses to genuine will be executed. To execute different explanations, bunch them inside a square proclamation ({ ... }) . All in all, it's great practice to consistently utilize square articulations, particularly when settling if proclamations:

if (condition) {statement_1_runs_if_condition_is_true;  statement_2_runs_if_condition_is_true; }  else { statement_3_runs_if_condition_is_false;  statement_4_runs_if_condition_is_false; } 

It is advisable to not use simple assignments in a conditional expression, because the assignment can be confused with equality when glancing over the code. For example, do not use the following code: if (x = y) { /* statements here */ } If you need to use an assignment in a conditional expression, a common practice is to put additional parentheses around the assignment. For example: if ((x = y)) { /* statements here */ }

while statement

Some time explanation executes its announcements up to a predefined condition assesses to genuine. Some time proclamation looks as pursues: while (condition) explanation If the condition turns out to be false, articulation inside the circle quits executing and control goes to the announcement following the circle. The condition test happens before explanation on top of it is executed. On the off chance that the condition returns genuine, explanation is executed and the condition is tried once more. On the off chance that the condition returns false, execution stops and control is passed to the announcement following while. 

To execute various proclamations, utilize a square articulation ({ ... }) to assemble those announcements.

Example: The following while loop iterates as long as n is less than three:

var n = 0;
var x = 0;
while (n < 3)
{ n++; x += n; }

With each iteration, the loop increments n and adds that value to x. Therefore, x and n take on the following values:


After the first pass: n = 1 and x = 1
After the second pass: n = 2 and x = 3
After the third pass: n = 3 and x = 6

After completing the third pass, the condition n &lt 3 is no longer true, so the loop terminates.

Function declarations

A function definition (likewise called a function presentation, or function explanation) comprises of the function catchphrase, trailed by: 

The name of the function. 

A rundown of contentions to the function, encased in enclosures and isolated by commas. 

The JavaScript explanations that characterize the function, encased in wavy sections, { }. 

For instance, the accompanying code characterizes a basic function named square: 

work square(number) { return number * number; } The function square takes one contention, called number. The function comprises of one explanation that says to restore the contention of the function (that is, number) duplicated without anyone else's input. The arrival articulation indicates the worth returned by the function 

return number * number; 

Crude parameters, (for example, a number) are passed to capacities by worth; the worth is passed to the function, however on the off chance that the function changes the estimation of the parameter, this change isn't reflected all inclusive or in the calling function.

Reference

All the documentation in this page for based of JavaScript.

No comments

Powered by Blogger.