[Gukulkan]

Learn, develop, play.

Buggy JavaScript Code: The 10 Most Common Mistakes JavaScript Developers Make

Today, JavaScript is at the core of virtually all modern web applications. The past several years in particular have witnessed the proliferation of a wide array of powerful JavaScript-based libraries and frameworks for single page application (SPA) development, graphics and animation, and even server-side JavaScript platforms. JavaScript has truly become ubiquitous in the world of web app development and is therefore an increasingly important skill to master.

Continue read >>>

How If Else using

For correct use if else you should to use next construction:


if (a==1) {
...
} else if (a==2) {
...
} else {
...
}

For example:


var a = 1;
if (a==1) {
//execute
} else if (a==2) {
//not
} else {
//not
}

var b = 2;
if (b==1) {
//not
} else if (b==2) {
//execute
} else {
//not
}

var c = 3;
if (c==1) {
//not
} else if (c==2) {
//not
} else {
//execute
}

Functions in JavaScript (js)

To create function in javascript we should write just:


function hello() {
}

For example lets call Alert with text Hello World :


function hello() {
alert("Hello World");
}

And call this function:


hello();

If you need to send some parameters in function, just write like this:


function hello(s) {
alert(s);
}
hello("Hello World");

We call this function with parameters and you can see “Hello World” in alert again.

Variables in JavaScript (js)

For declare variable in javascript we use var tag.

Example:

var x;

Numeric:

var x = 5;

Arithmetic:

var x = 4+1;

Fractional:

var x = 4.1;

String:

var x = "string";

Logical:

var x = false;

Arrays:

var x = new Array(1, "string", 5.3, false);

Add element to Array

Add a new single item to an array:


var books= ["book1", "book2", "book3", "book4"];
books.push("book5");

Add a lot of items:


var books= ["book1", "book2", "book3", "book4"];
books.push("book5", "book6", "book7");

General syntax:

.push(item1,item1,…,itemN)