JavaScript Tutorial

From GTA Connected
Revision as of 13:23, 14 August 2018 by Mex (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This page shows some of the functionality in JavaScript. Be sure to reference the official ECMAScript (JavaScript) manual when you can.

Comments

// this is a single line comment, which can appear at the end of any line

/* this is a multi line comment, which can appear anywhere

  • /

Functions

someFunction(); // this is a call to a function named someFunction, the code for that function will run when the function is called

someFunction(5, 400, 5000); // this is a call to a function, sending 3 parameters to it. a parameter is an input value for a function

// this is a function definition, which is a portion of code, don't forget the closing curly bracket to end the function definition function someFunction() { }

// this is a function definition, which takes 3 parameters. parameters are also called arguments function someFunction(arg1, arg2, arg3) { }

// this is a function without a name, also known as a lambda function function() {};

// this is a function which adds 2 numbers // the word return is used to set the output of the function, as well as to stop the function running at the expression after the word return // the result of add(..) is stored inside a variable named result function add(a, b) {

   return a + b;

} var result = add(40, 20);

Data Types

string 'this is a string' "this is a string"

number 20 40.0 99e5 -20 -40.0

boolean true false

null null

someVariable = null; // set a variable to null, the variable still exists

undefined delete someVariable; // delete a variable, the variable no longer exists, and evaluates to undefined

object {} // an empty literal object

{'aa':5, 'bb':10} // a object literal with 2 elements, element with key aa has value 5, and element with key bb has value 10

function SomeClass() { } var someObject = new SomeClass; // an object of class SomeClass

Data type symbol also exists.

Intrinsic Data Types

Array [20, 50, 80] // an array with 3 elements, keys are 0 1 and 2, values are 20 50 and 80, respectively.

Function function() {} // an empty function with no name

function someFuncName() // an empty function with a name { }

someFuncName = function() // an empty function with a name { }

Many other intrinsic data types exist, check section 6.1.7.4 of the JavaScript Manual.

Operators

Math Operators + - * / % // these are some mathematical operators

5 * 4 // results in 20

5 * 4 + 2 // results in 22

5 * (4 + 2) // results in 30

Variables

var someVariableName = 'some string value'; // this creates a local variable and assigns it a string value

someGlobalVariable = 50; // this either creates a global variable, or updates a variable, and assigns it a number value

Conditions

var someVariable = 500;

functipn print(text) {

   // code here

}

// if if (someVariable < 1000) {

   print('value is less than 1000');

}

// if and else if (someVariable < 200)

   print('value is less than 200');

else

   print('value is 200 or more');


// if, else if and else if (someVariable < 100)

   print('value is less than 100');

else if (someVariable < 300)

   print('value is less than 300');

else if (someVariable < 500)

   print('value is less than 500');

else

   print('value is 500 or more');

Loops

functipn print(text) {

   // code here

}

// integer loop for (var i=0; i<20; i++) {

   print(i);

}

// array loop var someArray = [20,50,80]; for (var i in someArray) {

   print(someArray[i]);

}