/*pattern examples*/
/^HTML/ // Match the letters H T M L at the start of a string
/[1-9][0-9]*/ /// Match a non-zero digit, followed by any # of digits
/\bjavascript\b/i // Match "javascript" as a word, case-insensitive
/*test*/
var text = "testing: 1, 2, 3"; // Sample text
var pattern = /\d+/g // Matches all instances of one or more digits
pattern.test(text) // => true: a match exists
text.search(pattern) // => 9: position of first match
text.match(pattern) // => ["1", "2", "3"]: array of all matches
text.replace(pattern, "#"); // => "testing: #, #, #"
text.split(/\D+/); // => ["","1","2","3"]: split on non-digits
function debug(msg){
var log = document.getElementById("debuglog");
if(!log){
log = document.createElement("div"); // Create a new <div> element
log.id = "debuglog"; // Set the HTML id attribute on it
log.innerHTML = "<h1>Debug Log</h1>"; // Define initial content
document.body.appendChild(log); // Add it at end of document
}
var pre = document.createElement("pre"); // Create a <pre> tag
var text = document.createTextNode(msg); // Wrap msg in a text node
pre.appendChild(text); // Add text to the <pre>
log.appendChild(pre);
}
debug("test");
//ARITMETIC
3-2
3*2
3/2 // 1.5:
3%2 //1: module
points[1].x - points[0].x;
"3"+"2" //"32": contatenates strings
//UNARY OPERATORS
var count=0;
count++;
count--;
count+=2; //add 2 same as count = count + 2;
count*=3; //count = count*3;
i=1; j=i++;//1,2
i=1; j=++i;//2,2
//RELATIONAL
if ((x == 0 && y == 0) || !(z == 0))
var x=2,y=3;
x==y // false
x!=y //true
x<y //true
x<=y //true
"two"=="three" //false
"two" > "three" //true: "tw" is alphabetically greaterthan "th"
(x>y) == false //true: false is equal to false
1 == true//true
/*Objects are not compared by value: two objects are not equal even if they have the same
properties and values. And two arrays are not equal even if they have the same elements
in the same order.
object values are references, and we say that
objects are compared by reference: two object values are the same if and only if they
refer to the same underlying object.
*/
var a = []; // The variable a refers to an empty array.
var b = a; // Now b refers to the same array.
a === b // => true: a and b refer to the same object, so they are equal.
/*ESTRICT EQUALITY ===
-If the two values have different types, they are not equal.
-If one or both values is NaN, they are not equal. The NaN value is never equal to any other value, including itself!
-If both values refer to the same object, array, or function, they are equal.
*/
/*EQUALITY ==
-If one value is null and the other is undefined, they are equal.
-If one value is a number and the other is a string, convert the string to a number and try the comparison again, using the converted value.
-If either value is true, convert it to 1 and try the comparison again. If either value is false, convert it to 0 and try the comparison again.
*/
//LOGICAL
(x==2)&&(y==3) //true: and
(x>3) || (y<3) //false: or
!(x==y) //true: !inverts a boolena value
var a=[],b=[];
console.log(a==b) //false: two objects are not equal even if they have the same properties an values
var x={};
var y=x;
console.log(x==y)//true: they refer to the same object
var d = new Date(); // Create a new object with the Date() constructor
d instanceof Date; // Evaluates to true; d was created with Date()
d instanceof Object; // Evaluates to true; all objects are instances of Object
d instanceof Number; // Evaluates to false; d is not a Number object
var a = [1, 2, 3]; // Create an array with array literal syntax
a instanceof Array; // Evaluates to true; a is an array
a instanceof Object; // Evaluates to true; all arrays are objects
a instanceof RegExp; // Evaluates to false; arrays are not regular expressions
//OBJECT
var book={
topic:"JavasScript",
fat:true
};
//TYPEOF returned a string with type
typeof value == 'undefined'//true
/*GERERATE DINAMIC VARIABLES*/
var num=1;
window['myvar' + num] = "Hello Word";
/*
-EXPRESSIONS: are evaluated to produce a value;
-STATEMENTS: are execueted to make something happen.
*/
/*CONTROL STRUCTURES*/
//------------------------------------------------------------------------------
for(var i=0;i<10;i++){
console.log(i);
}
for(var i=0,j=10;i<10;i++,j--){//double statement
console.log(i*j);
}
/*array values*/ç
for(var i = 0; i < a.length; i++){
console.log(a[i]);
}
/*object properties*/
for (var key in data){
console.log(key+": "+data[key]);
}
//EMPTY STATEMENT
//clear array
for(i=0; i<list.length; list[i++]=0) /*empty*/;
//------------------------------------------------------------------------------
if(n==1){
/*...*/;
}else if(){
/*...*/;
}
//------------------------------------------------------------------------------
var count = 0;
while (count < 10) {
console.log(count);
count++;
}
//------------------------------------------------------------------------------
do {
console.log(a[i]);
i++;
} while (i < list.length);
//------------------------------------------------------------------------------
switch(n){
case 1: //if(n==1)
break;
case 2:
case 3:
break;
default:
break;
}
function convert(x) {
switch(typeof x) {
case 'number': // Convert the number to a hexadecimal integer
return x.toString(16);
case 'string': // Return the string enclosed in quotes
return '"' + x + '"';
default: // Convert any other type in the usual way
return String(x);
}
}
//JUMPS
break //jump to the end of a loop or other statement
continue //skip the rest of body of a loop an begin a new iteration
return //jump from function invocation back to the code, and return a value
throw//create an exception
myLabel://point of the code to begin before break
mainloop: while(token != null) {
// Code omitted...
continue mainloop; // Jump to the next iteration of the named loop
// More code omitted...
}
//VALIDATIONS
if (username == null)//null or undefined
if (!username)//null, undefined, false, 0, "", or NaN
if (window.jQuery)//global vars
if (typeof x == 'function')//type o values
if (obj instance of MyClass)//check class
obj.constructor //get Class Name
{}.toString.call(obj)//explicity THIS: objeto.metodo.call(invoker);
NAMING CONVENTION
myVar;
$myjQueryVar;
_myPrivateVar;
SCOPE
Javascript does not have block scope, variables declared within a statement block are not private to the block.
for(var i=0;i<3;i++){
};
console.log(i); //3:
VARIABLE AS PROPERTIIES (VAR) If we use "var" to declare, the property that is created is nonconfigurable, which means that it cannot be deleted by "delete" operator. "var" statement appears within the body of a function, it defines local variables, scoped to that function.
function() = {
var myVar;
}
"var" is used in top-level code, it declares global variables,visible throughout the JavaScript program , we are defining a property of the global object "window". var myVar; function()={ .. }
/TYPE -PRIMITIVE (INMUTABLE) types(numbers,string,boolean,null,undefined) -OBJECT (MUTABLE)(object,array,function)/ typeof undefined//"undefined" typeof 0//"number" typeof true//"boolean" typeof "text"//"string" typeof {}//"object" typeof null//"object" typeof function(){}//"function" typeof NaN//"number"
//FALSY VALUES types values convert to boolean FALSE undefined,null,0,-0,NaN,"" <=> (!myVar)
//TRURTHY VALUES function,1,object,array(is an object)
var x=0;
var x="hello word" <=> new String("hello word");
var x='1\\3'; //1\3 : escape sequence
var x = (a || b || c); console.log(0 || 1 || false)//1: return the first trurthy value console.log(false || undefined || null)//null: if all values are falsy return the last.
var x =(a && b && c); console.log( 0 && 1 && false)//0;//return the first falsy value console.log(true && {} && 1)//1;if all values are trurthy return the last
a = b = c = 0;//multiple assignment
var greeting = "hello " + (username ? username : "there")//Ternary operator
(typeof value == "string") ? "'" + value + "'" : value;
function display_object(o) {
if (!o) return;//if no return value, return undefined
}
/UNDEFINED VS NULL/ var x = null; //type=>object,value=>null var x; //type=>undefined,value=>none /*null -is the better option to pass an empty variable. / /undefined: -if you don't specify an initial value -the value of an object property o array, does not exist. -function that have no return value.ç -function parameters for which no argument is supplied. */
/FLOAT PRESICION/ var x = .3 - .2; // thirty cents minus 20 cents var y = .2 - .1; // twenty cents minus 10 cents x == y // => false: the two values are not the same! x == .1 // => false: .3-.2 is not equal to .1 y == .1 // => true: .2-.1 is equal to .1
/STRING/ //multiline var text='hola'+ 'mundo';
var text='hello
word';
console.log("one \n two"); //one //two
//transform var s = "hello"; s = s.toUpperCase();//string are immutable, the method return a new string.
/WRAPPER OBJECTS/ //(The temporary objects created when you access a property of a string, number, or boolean) var s = "test"; var word = s.toString();// <=> var word = new String(s):
//example var s = "test"; // Start with a string value. s.myProp = 4; // Set a property on it. var t = s.myProp; //t= (new String(s)).myProp: myProp does not exist in the new object. console.log(t);//undefined. //conclusion: string,number and boolean are read-only, we can't define new properties on them.
/TYPE CONVERSIONS/ 10 + " objects" // => "10 objects". Number 10 converts to a string "7" * "4" // => 28: both strings convert to numbers var n = 1 - "x"; // => NaN: string "x" can't convert to a number n + " objects" // => "NaN objects": NaN converts to string "NaN"
0false //true: boolean converts to number before comparing "0"0 //true: string converts to a number before comparing "0"===0 //false:strict equality operator 2+undefined // NaN true + true //2 "1"+2//12
var x ="hola"; (!!x)//true: convert any value to boolean
//EXPLICIT CONVERSIONS Number("3") //3 String("false") //String Boolean([]) //true Object(3) //Number
/Object to Primitive Conversions/ new Boolean()//false: new Boolean({})//true: objects, functions and array are turthy values.
/USE STRICT/ http://raohmaru.com/blog/javascript/use-strict/ http://www.w3schools.com/js/js_strict.asp