-
Notifications
You must be signed in to change notification settings - Fork 0
JavaScript Basics
- Shortcuts
- Variables
- Data Types
- Truthy and Falsy Values
- Operators
- Decision making
- Funcions
- Arrays
- Objects
- Loops
- Hoisting Scope and This
- Ctrl+Shift+J otvara console u Chrome-u
- cl - console.log(obj);
- al - alert('msg');
- pm - prompt('msg');
- fun - function block
-
Single line comments //
-
Multi line comments /**/
-
Always use camelCase
-
Only can start with letter, $ or underscore
-
Cannot give variables reserved names
-
Declare multiple variables in one line and assign values later
var job, isMarried; job = 'programmer'; isMarried = true;
-
When we want to change value of any variable we don't need to use var keyword, but only name and new value
-
Concatenation is done with + sign
-
Multiple assignment of value can be done by simply typing
var x, y; //Check Operators, Table of Precendece for Assignment Operator '=' x = y = (3 + 5) / 2;
- Number - floating point, decimals, integers
- String - sequence of characters used for text
- Boolean - true/false
- Undefined - variable does not have a value yet
- Null - non existant
- Falsy values - return false when evalueted in if/else conditions
- Undefined
- Null
- 0 - avoid by adding condition that variable === 0 when true
- ''
- NaN
- Thruty values - return true when evalueted in if/else conditions
- all not falsy values
-
Math Operators - returns result
- + - * /
-
Logical Operators - returns true, false
- <, >=, <=, >
-
typeof Operator - returns type of variable
-
Assignment Operators Short version for adding same variable +-*/ and =
x = x + 2; ==> x += 2; x = x * 2; ==> x *= 2; x = x / 2; ==> x /= 2; x = x - 2; ==> x -= 2;
-
Increment Operator ++ or --
x = x + 1; ==> x++; x = x - 1; ==> x--;
-
Boolean logic
- AND & & true if all are true
- OR || true if one is true
- NOT ! inverts true/false value
-
Ternary operator used for making if/else statements in one line. Can be assigned to variable.
condition ? //statement_if_is_true : //statement_if_is_false; var name = condition ? //statement_if_is_true : //statement_if_is_false;
-
== vs ===
- == is less strict, it transforms data type in order to make it true 23 == '23';
- === does data type comparison -> data type must be same in order to be true 23 !== '23';
- If/elseif has this structure:
if (condition1) {
//statement1;
} else if (condition2) {
//statement2;
} else if (condition3) {
//statement3;
} else {
//statementN;
}
-
Switch statements, if we work with ranges we need to put true instead of valiable
switch (variable) { case expression1: case expression2: //statement1 break; case expression3: //statement2 break; default: //statement default }
-
Declaring a function statement(they do actions- don't produce any immediate value):.
function functionName(arg1, arg2){ //what function does //return }
-
Calling the function
functionName(arg1, arg2);
-
Declaring a function expression (anything that produces immediate result):
var functionName = function(arg1, arg2) { //what function does //return }
-
Two ways of creating an array
var arrName = [element1, element2]; //or var arrName = new Array(element1, element2);
-
Accessing the array or its [elements]
arrName[index];
-
Usefull array functions and methods
//number of elements arrName.length; //add to end of array arrName.push(element); //or arrName[arrName.length] = element; //add to the begining of the array arrName.unshift(element); //remove element from the end of the array arrName.pop(); //remove element from the begining of the array arrName.shift(); //return the position of the argument that we pass inside array -> **returns -1** if there is no such element arrName.indexOf(element);
-
Used for grouping variables which go together in no particular order
-
Defining object
var objName = { keyString: 'value', keyNumber: 192, keyArray: [], keyObject: { keyOfInsideObject: 'value' } funct: function(Arg){ return something; } } //or var jane = new Object(); jane.name = 'Jane'; jane.birthYear = 1992; jane[''lastaAme] = 'Austin';
-
Defining objects as function constructor, always Capital Letter
var Name = function(arguments){ this.arg1 = arg1; this.arg2 = arg2; } //popunjavanje var objName = new Name(arg1, arg2); //dodavanje metoda na prototip Name.prototype.functionName = function () { //do anything }; // dodavanje propertija na prototip --> Svi se prezivaju smith! Not common Name.prototype.lastName = 'Smith'
- new creates new empty Object and this se odnosi na ono u novom praznom objektu
- Da li ima SVOJ property Name.hasOwnProperty() ne vraca inherited
- name.instanceof Object da li je instanca
- console.info(element) vraca prototip sa svim metodima
-
Object.create metod
-
Accessing object and values and editing it by declaring it with =
console.log(objName.objKey); //or -->without '' if it is a number console.log(objName.['objKey']); //or by declaring a variable with keyName var a = 'keyName'; console.log(objName.[a]); //edit object objName.keyName = newValue; //access function objName.funct(Arg);
-
Usefull objects methods
//this je trenutni objekat this.keyName; //example-> napravi ili edituj age key i daj mu vrednost 2018- godina rodjenja tog objekat this.age = 2018- this.birthYear;
-
For Loop
for(initialCounterValue = x; condition; counterUpdate){ //what to do if all 3 conditions went well }
-
While Loop
var counter = innitialValue; while(condition){ //do while condition is true counterUpdate; }
-
Continue and Break Statements
continue //-> kada se stavi pored if statementa za neki uslov,ako naidje na slucaj kada nije ispostovan uslov samo ce da nastavi dalje, nece nista raditi sa tim slucajem break //-> kada se stavi pored if statementa za neki uslov,ako naidje na slucaj kada nije ispostovan uslov sne nastavlja dalje nego prekida izvrsavanje
-
When we declare function we can use them even before they are declared in code because it is hoisted in JS.
funcName(arg); function funcName(arg){ //what it does }
-
When we try to use variable before its declaration in code it will be undefined, but it will exist. That is beacause on the execution start code scans all variables and sets them to undefined.
-
Scoping works from bottom to top. Not in opposite direction. Local definitions are never accessible to global ones if they are not returned from functions.
-
Where does this point
- regular function call -> on global object
- method call -> object that is calling the method, method is function inside an Object
- in function call -> on global object -in case of browser -Window
Written by Ninna94