This tutorial is for beginners in javascript and web development who don't like to spend hours learning a programming language.you need to learn some html and CSS basics before starting.I tried to simplify things as much as possible to better understanding of this amazing language.
What Is javascipt ?
-Javascript is the programming language of the web , originally launched by Netscape. it's a server side language that make html and css pages more interactive.-Most websites in the web ,today , use javascript .including facebook ,yahoo,google ...
-Java and Javascript are totally different even if you find some syntax similarity.
-Javascript syntax is inspired by the C language syntax.
How to embed Javascript in a web page ?
-Javascript code must be added inside a script tag into the Html document :Note : the script above can be integrated in a Html5 document ,for Xhtml or Html it's better to add the type attribute :
the script can also be external ,in this case create a .js file which contain the code ( without the script tag) and then refer to it in the Html document using the src attribute :
-the script can be added everywhere inside the html tag , but usually we put it inside the head tag.
Javascript Syntax :
-Remember that javascript is case sensitive , "Articmo" is totally different to "articmo".-Each code statement end with ";" , I recommend to use this symbol to keep your code clear,but it's optional.
-Javascript comments are optional and not executed , there are two ways to add comments :
*If the comment is a single line then use the two slash "//"
*If the comment is more than one line then add it inside this block : /* ..... */
-the two symbols " and ' are the same , but you must use one type in a single statement.
Javascript variables :
Declaring a variable in javascript can be performed by the keyword var ,example:you can declare a variable without adding a value .like the i variable above, In this case the type of i is Undefined.
One other important thing in javascript is the dynamic aspect of its types , in other words the type of variable can be changed in the code ( the example of the j variable above).
Data Types :
Javascript has seven types of data : String,Number,Array,Object,Boolean,Undefined,Null.The code below explain the way to declare these types :
Notes :
-The keyword "new" is used to declare the variable with it's type .
-The first character in each type must be a capital letter , "String" and not "string".
-The variable without a value has Undefined as type.
-Null is used to void a variable.
-Javascript has an interesting method to extract the type of any variable : typeof , example :
-"5" and 5 are totally differents , the first one is a String , however the second is a Number.
-As you can see , There are many ways to declare variables in javascript .
Javascript Operators :
Operators are commonly used in javascript.check the list below := add a value to a variable , not the arithmetic equal.
+ addition of two numbers or strings.
* - / Arithmetic operators.
++ increment a Number.
-- decrement a Number.
+= , -= , /= and *= (explained in the code below)
Comparison and logical Operators :
Comparison and logical operators always return a Boolean value (true or false) :x == y : if x is equal to y return true else false.
x === y : if x is equal to y in type and value return true else false (see the example below).
x < y , x > y , x < = y , x >= y : return true if the comparison is true else false.
x != y: not equal , if x is equal to y , return false else false.
x !== y : not equal in type and value.
x == y || x <y : the OR operator , if one of two statements return true or only one true then it return true , if the two are false it return false.
x == y && x > y : this is the and operator , the two statements must be true to return true .
!(x>=y) : this is the NOT operator which test the opposite of the statement, in this case , if x>=y it return false else true.
Javascript functions: :
One of the important component in javascript is the function.the function is a set of statements that execute together . here is the basic syntax of a function in javascript :
-function always start with the keyword function.
-nameFunction is the name of the function , you can choose your owen , but it must begin with a letter or '_' as for strings.
-parameters must be putted inside parentheses, it can be empty (without parameters) , one parameter or more than one (separated by comma)
-the function code and statements must be putted inside the semilicon block { } .
-to execute or call the function just insert the name of the function with the required parameters.
Notes :
-the prompt( ) method display a popup with a textfield .usually used to enter data.
-the Alert ( ) method is used to display data.







0 comments
Posts a comment