CodeGym/Java Course/Module 3. Java Professional/Basic Concepts in JavaScript

Basic Concepts in JavaScript

Available

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
var pi = 3.14;
Contains any number
2 String
var s = "Hello!";
Contains a string
3 Boolean
var result = true;
Contains true or false
4 array
var arr = [1, 2, 3, 4, 5];
Contains an array of elements
5 Date
var current = new Date();
Contains date
6 Object
var o = {
   width: 100,
   height: 200
}
Contains an object that consists of key, value pairs. Something similar to HashMap in Java
7 function
function sqr(var x) {
   return x*x;
}
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
Comments (1)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Hoist
Level 35 , San Diego, United States
31 October 2023, 22:32
1) arrays also behave like collections - you can dynamically add elements to them 2) objects in JavaScript are very similar to HashMap in Java: they contain key-value pairs