Git Product home page Git Product logo

basicjs's Introduction

Variables

You can write var with letters, numbers, underscores and dollar sign, but the first sign had to be a letter or dollar sign.

// Examples name of vars
var n2_0 = 'red';
var myNum = 30;
var &width = 300;

// type number
var i = 35;

// type string
var i = 'nice var';

Dom Manipulation

Handled HTML Elements

// ID
document.getElementById("nameOfID");
// TAG HTML
document.getElementsByTagName("p");
// CLASS CSS
document.getElementsByClassName("nameOfClass");

Elements of document

// BODY
document.body
// TITLE'S DOM
document.title
// ALL IMAGES'S DOM
document.images
// ALL LINKS'S DOM
document.links
// ALL SCRIPTS'S DOM
document.scripts

Style

**Apply propeties css by js ** element + style + propertie css + value css

document.body.style.background = "red";

Functions

Auto-execute javascript functions AND call them later

(function(){

// here your functions auto-executed

}());

Arrays

Multiple values in one var.

// Array
var colors = ['red', 'blue' 'purple']

Position of a element in array

// Choose purple element of this array
var colors = ['red', 'blue' 'purple'];
colors[2];

Change value of a element of array

// Purple will be green
var colors = ['red', 'blue' 'purple'];
colors[2] = 'green';

How many values are in an Array

// length propertie
var colors = ['red', 'blue', 'purple', 'cyan', 'skyblue'];
colors.length;

Show the inverse values of an Array

// reverse propertie
var colors = ['red', 'blue', 'purple', 'cyan', 'skyblue'];
colors.reverse();

Loops

Repite acctions

The FOR loop

// Repeat x times an action
for (var i=0; i<10; i++ ){
   console.log('this time is ' + i);
};
// Execute a provided function once per array element.
var funArray = [11, 27, 38, 42, 57];
for (var i=0; i < funArray.length; i++ ){
   console.log(funArray[i]);
};

The While loop

// Repeat an action while a condition
var i = 0; 
while ( i < 10) { 
   console.log(i++)
}

The forEach loop

// Execute a provided function once per array element.
var funArray = [11, 27, 38, 42, 57];
funArray.forEach( function(i) {
   console.log(i);
});

Conditionals

Conditionals power

if

// If it's equal
var i = 2;
if(i == 2){
console.log('yes,' +i+ ' is equal 2');
}
// If it's not equal
var i = 2;
if(i != 4){
console.log('You are right,' +i+ ' is not equal 4');
};

if else

// If it's not equal
var i = 4;
if(i != 4){
  console.log('You are right, ' +i+ ' is not equal 4');
} else {
  console.log('You are wrong, ' +i+ ' is equal 4');
};
// If X is not equal or more than X
var i = 3;
if(i != 4 || i < 4){
  console.log('Ummm, ' +i+ ' is not equal 4 or it is more than 4');
} else {
  console.log('Ummm, ' +i+ ' is equal 4');
};

switch case

Matching the expression's value to a case clause, and executes statements associated with that case.

var cars = 'audi';
switch(cars){
   case 'audi':
     console.log('It is A4');
	 break;
	 case 'ford':
     console.log('It is my car Ford');
	 break;
	 case 'ferrari':
     console.log('It is the slow car of Fernando Alonso');
	 break;
}

Objects

Simple Object

var person = {
  name: 'mike',
	age: 20,
}
console.log(person.name);

Simple Object with Array

var person = {
  name: 'mike',
	age: 20,
	daughters: ['Linda', 'Maggie'],
}
console.log(person.daughters[1]);

Complex Object

Object with other object inside

var person = {
  name: 'mike',
	age: 20,
	address: {
	   street: 'Velazquez 10',
		 city: 'Madrid'
	}
}
console.log(person.address.city);

Tips

JQUERY: Multiple CSS()

$selector.css({
   'font-size' : '10px',
   'width' : '30px',
   'height' : '10px'
});

JS: default value to an undefined parameter

function functionWithDefaultValue(b){
    b = b || 123;
    return b;
}

// Result without parameter -----> Result 123
functionWithDefaultValue()   
// Result with parameter    -----> Result 66
functionWithDefaultValue(66) 

JS: Return multiple values from a function

function operation(val1, val2){
	sum = val1 + val2;
	sub = val1 - val2;
	return {
		sum: sum, 
		sub: sub
	}; 
};
console.log( operation(20,5).sum ); // 25
console.log( operation(20,5).sub ); // 15

basicjs's People

Watchers

James Cloos avatar Fernando Muñoz avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.