2.1 Variables and pain
Let's start with the most interesting. JavaScript has variables, but those variables don't have a type. Any variable can be assigned absolutely any value. Looks innocuous or even handy until you need the types.
The keyword is used to declare a variable var
:
var name;
var name = value;
Examples of working with variables in JavaScript:
var a = 10, b = 20;
var c = a*a + b*b;
var s = "Diagonal equals:";
console.log( s + Math.sqrt(c));
Great and clear code, isn't it? Perhaps this is the last time in your life when you see beautiful and understandable JavaScript code. Remember this moment :)
2.2 Typing in JavaScript
As mentioned above, variables in the JavaScript language do not have a type. But the values of variables have types. Here are the 5 most common types in JavaScript:
# | Type | Example | Description |
---|---|---|---|
1 | number |
|
Contains any number |
2 | String |
|
Contains a string |
3 | Boolean |
|
Contains true or false |
4 | array |
|
Contains an array of elements |
5 | Date |
|
Contains date |
6 | Object |
|
Contains an object that consists of key, value pairs. Something similar to HashMap in Java |
7 | function |
|
Function |
The typeof keyword is used to determine the type of an object, for example:
var s = "134";
var x = (typeof s == "String") ? s*1 : s;
2.3 Functions and returns
And of course JavaScript has functions. There are no classes, so functions can be declared anywhere in the code. Even in other functions. The general format is:
function name(a, b, c) {
// function code
return result;
}
The function has no type. Why, if there is no type compatibility control in the language itself? Function parameters may also be missing. So is the return command, which returns a value.
When calling a function, you can pass any number of parameters of any type . The excess will be discarded, the missing ones will be equal null
.
Function examples:
function getValue(name)
{
return this[name];
}
function setValue(name, value)
{
this[name] = value;
}
2.4 Arrays in JavaScript
Arrays in JavaScript are very similar to arrays in Java. Examples:
var array = [1, 2, 3, 4, 5];
array[3] = array[2];
console.log (array[0]);
They can have values of any type, even other arrays:
var array = [1, "Hello", 3.14, [4, 5] ];
array[3] = array[2];
console.log (array[0]);
In addition, arrays also behave like collections - you can dynamically add elements to them:
var array = [];
array.push(100);
array.push(101);
array.push(102);
array[1] = array[2];
console.log (array[0]);
2.5 Objects in JavaScript
Objects in JavaScript are very similar to HashMap in Java: they contain key-value pairs. Example:
var obj = {
name: "Bill Gates",
age: 67,
company: "Microsoft"
};
console.log (obj.age);
Object fields can be accessed in two different ways:
var x = obj.age;
var x = obj["age"];
As with HashMap, fields can be created and deleted. Example:
var obj = {};
obj.name = "Bill Gates";
obj.age = 67;
obj.company = "Microsoft";
delete obj.age; //remove field
GO TO FULL VERSION