ostra
1/16/2018 - 8:53 AM

JavaScript Short Reference

//diplay alert
alert('');
//give user opportunity to enter some values
prompt('');
//for user to say yes or no
var result = confirm("Завершить выполнение программы?");
if(result===true)
  document.write("Работа программы завершена");
else
  document.write("Программа продолжает работать");
//display info to console
console.log('');
//to display info on a page
document.write('');
//to display on console var type
console.log(typeof var);
//browser history
history.length
history.back();
history.go(-2);
history.go(3);
//page location
location.href, location.pathname, location.origin, location.protocol,
location.port, location.host, location.hostname, location.hash, 
location.search
//geolocation
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
var altitude = position.coords.altitude;
var speed = position.coords.speed;

//logical perations
//compare values
//==, !=, >, >=, ...
//compare values and types
//===, !==

//to comvert strings
parseInt(var);
//to convert to other numeric systems
parseInt(var, 2);
parseFloat(var);
//to check if string contains number
isNaN();

arr.indexOf(); .lastIndexOf(); .substring(12, 4);
.toLowerCase(); .toUpperCase(); .charAt(2); .charCodeAt(2);
.replace('aaa', 'bbb'); .string1.concat(string2);

//create array
var arr = [['', '']], ['', '']];
console.log(arr[0][0]);
//array length
arr.length;
//add element to the end of array
arr.push( );
//add element to the beginning of array
arr.unshift( );
//delete last element
arr.pop();
//delete first element
arr.shift();
//delete by index
arr.splice(1);
//to connect to elems
arr1.concat(arr2);
//copy arr part
arr.slice(1,4);
//arr elements to string
arr.join()

//if clouse
if() ...;
if(){
  ...;
} else if {
  ...;
} else ...;
//if true - first case, if false - second
var result = (a < b) ? (a + b) : (a - b);
//switch construction
switch(var){
    case 100 : 
        ...;
        break;
    case 200 : 
        ..;
        break;
}
//for
for (var i = 0; i < ...; i++){
  ...;
}
//for in
for (var i in arr){
  ...;
}
//while
while(){}
//do while
do{}while();
//get out of cycle
break;
//to miss one operation and stay in cycle
continue;

//simple function with return
function functionName(params){
  ...;
  return ...;
}
var some = functionName;
some();
functionName(5);
//immidiatly invoked function
(function(){});
//changes in function after calls
function display(){
    console.log("Доброе утро");
    display = function(){
        console.log("Добрый день");
    }
}
//recursive functions
function getFactorial(n){
    if (n === 1){
        return 1;
    }
    else{
         
        return n * getFactorial(n - 1);
    }
}
//hoisting of var - calling before declaring
console.log(foo); //after first call going to be undefined
var foo = "Tom";
//hoisting of func
display(); //going to work properly from the beginning
function display(){
    console.log("Hello Hoisting");
}

//new object
var user = {};
//parameters
user.name = "Tom";
user.age = 26;
//methods
user.display = function(){
  console.log(user.name);
  console.log(user.age);
};
//method call
user.display();
//alternativ to object ceration
var user = {
  name: "Tom",
  age: 26,
  display: function(){
    console.log(this.name);
    console.log(this.age);
  }
};
//this - to use parameters and methods in method
//to deleta parameter
delete user.name;

//objects in objects
var country = {
  name: "Германия",
  language: "немецкий",
  capital:{
    name: "Берлин",
    population: 3375000,
    year: 1237
    }
};
console.log("Столица: " + country.capital.name); // Берлин
//arrays as parameters
var country = {
  cities: [
    { name: "Цюрих", population: 378884},
    { name: "Женева", population: 188634},
    { name: "Базель", population: 164937}
  ]
};
//to check if parameter exists in object
"name" in user;
//object constructor
function User(pName, pAge) {
  this.name = pName;
  this.age = pAge;
  this.displayInfo = function(){
    document.write("Имя: " + this.name + "; возраст: " + this.age + "<br/>");
  };
}
var tom = new User("Том", 26);

//inheritance from method to another
function User (name, age) {
    this.name = name;
    this.age = age;
    this.go = function(){document.write(this.name + " идет <br/>");}
    this.displayInfo = function(){
        document.write("Имя: " + this.name + "; возраст: " + this.age + "<br/>");
    };
}
function Employee(name, age, comp){
    User.call(this, name, age);
    this.company = comp;
    this.displayInfo = function(){
        document.write("Имя: " + this.name + "; возраст: " + this.age + "; компания: " + this.company + "<br/>");
    };
}

//obect date
var currentDate = new Date();
var myDate = new Date("27 March 2008");
myDate.getDay();
myDate.getMonth();
myDate.setDate(15);
myDate.setMonth(6);
//other objects
Math.abs( );
Math.max( , );
Math.min( , );
Math.ceil( . );
Math.floor( . );
Math.round( . );
var x = Math.random();
Math.pow(2, 3);
Math.sqrt();
Math.log();
Math.PI;
Math.E;

//timer repeating till clearTimeout
function timerFunction() {
    document.write("выполнение функции setTimeout");
}
var timerId = setTimeout(timerFunction, 3000);
clearTimeout(timerId);
//timer repeating forever
function updateTime() {
    document.getElementById("time").innerHTML = new Date().toTimeString();
}
setInterval(updateTime, 1000);

getElementById(value)
getElementsByTagName(value)
getElementsByClassName(value)
querySelector(value)
querySelectorAll(value)

documentElement
body
images
links //a and areas
anchors //a with name attribute
forms