
Java and JavaScript are not the same thing! Now you know the main secret of the Masons. In this article I want to share with you thoughts and thoughts about the JavaScript language through the eyes of a Java developer. I will compare the data structures, types, functions, objects, classes, and other common features of Java and JavaScript.
Foundation
JavaScript is an interpretable, scripting language for writing scripts. This means that the code you write will be executed line by line, from the instructions in the instructions, from script to script.
Java is a compiled language, which means that before starting a Java program, the compiler must translate all the code written by you into special machine code that is understandable for the JVM -
bytecode .
The disadvantages for a Java developer who started writing on JS are related to these differences:
')
- Before starting the compilation of your code written in Java, the compiler will conduct a syntax and semantic analysis for you, and in case of problems, it will inform you about it. It turns out that you have the opportunity to learn about the error even before launching the application. In JS, due to the absence of a compiler, there is no such check. And the mistakes you made at the time of writing the code will be detected only after the launch of the script.
- The following inconvenience is possible only for the version of JS to ES5. Since JavaScript executes line-by-line writing style of functions that cause other functions to be different from the principles of Bob’s Uncle Clean Code preached for writing Java programs. In JS, if you need to call bar () from the foo () function, you must define bar () before foo () . If you do the opposite, as in Java applications, the call may fail in some older browsers.
Variables and their types
JavaScript is a weakly typed language, unlike Java. On the one hand, it gives more flexibility, on the other - more opportunities to make a shot in the leg. To declare a variable in JS, it is enough to use the
var keyword, after which specify the name of the variable and, if necessary, the value. In practice, it is not even necessary to use the
var keyword.
var name_1 = "Igor Ivanovich"; name_2 = "Renat Raphaelevich";
Now about the types. In JS for integer and floating point numbers there are no different types. They are united in the
number type.
String ,
boolean are the same as in Java. In JS, there is a type
Object . If in Java it is the superclass of all classes, then in JS it is just one of the types.
var a_number = 10;
Java developer should not confuse. Now let's try to conduct operations between objects of different types and see what happens.
10 +1 > 11 10 +"1" > "101" true && false > false true && 1 > 1 true && 0 > 0 true && "1" > "1" false && "1" > false false && 0 > false 0.1 + 0.7 > 0.7999999999999999
Some results may confuse a Java developer. For example, the ability to use the type of
boolean in this way:
true && 1 and still get some result. It is impossible to perform such an operation in Java, since the compiler will generate an error stating that the && operator cannot be used with non-
boolean types. Now let's turn our attention to another difference between JS and Java:
the === and
! == operators . These are the comparison operations required for a weakly typed language, such as JS.
=== - returns true if the compared objects are equal in value and their types are the same.
! == , respectively, returns true if the compared objects are not equal in value or their types do not match. Consider a few examples:
10 == 10 > true 10 == "10" > true 10 === "10" > false 10 != "10" > false 10 !== "10" > true 10 !== 10 > false
Functions
In JS, as in Java, functions can return / not return a value, can be with or without arguments. You can call a function in JS code, as was shown in the example above, or by reacting to a specific event on the html markup element.
<!-- --> <input type="button" value="CLICK" onclick="foo();"> // foo() function foo() { document.write("Calling foo"); }
Clicking the button on the page will print “Calling foo” as a result of the function call. Now about the weirdness that a Java developer can pay attention to. Let's return to the example above, where the function
foo () calls the function
bar () in itself - which is used only as an internal function. According to the idea, we expect that it can be made private. But there are no access selectors in JS. There is no way to make a
private field simply by adding this word before declaring a function. Let's go further. Create an object - an instance of a class with its own fields and one method.
function getLocation() { if (this.person === "Igor" && this.age > 25) { document.write("Your name " + this.person + ", location = Vishnevaia 1"); } else { document.write("Your name " + this.person + ", location = rp Sokoloviy"); } } function Person(person, age) { this.person = person; this.age = age; this.getLocation = getLocation; } var igor = new Person("Igor", 26); igor.getLocation(); document.write("<br />"); getLocation();
If you look at this code through the eyes of a Java developer, you can note that the
Person function is the constructor of the objects of the
Person class and the definition of fields and methods included in the class. The
getLocation () function is a function of the
Person class. Inside it, we use a reference to the fields of the instance of the class
this.person and
this.age . Logically, this function, using the current instance of the
Person class, should work only with it and the last call to the
getLocation () function should not work. But, in JS this is normal, because the concepts of class, function, class methods are blurred. Weak typing in everything. By running this script, you get the following output in a browser window:
Your name Igor, location = Vishnevaia 1 Your name undefined, location = rp Sokoloviy
However, rewriting the code as follows, defining a function inside a class, its call not for an instance of the class will be unavailable:
function Person(person, age) { this.person = person; this.age = age; this.getLocation = function () { if (this.person === "Igor" && this.age > 25) { document.write("Your name " + this.person + ", location = Vishnevaia 1"); } else { document.write("Your name " + this.person + ", location = rp Sokoloviy"); } }; } var igor = new Person("Igor", 26); igor.getLocation(); document.write("<br />"); getLocation();
The last call will lead to an error, because The
getLocation () function is not defined. It turns out that at least in JS and there are no access modifiers, but there is a scope of functions and variables, controlled using curly brackets. JavaScript is beautiful with a huge number of options to shoot yourself in the leg.
Arrays
When we talk about arrays, we present a data structure that stores similar elements that are accessed by index. This is in java. When it comes to JS and its weak typing, a real anarchy comes into play. In the following example, we create 4 arrays. In the first one there are elements of different types, in the second only numbers, in the third
boolean , in the fourth
boolean and
number :
var mix = [3, "Igor Ivanovich", "Renat Raphaelevich", "Sergey Sergeevich", 1, 12.3, true]; var numbers = [1,2,3,4,5]; var booleans = [false, false, true]; var mix2 = [false, 1, 2]; document.write("Type elements in mix: "); for (element in mix) { document.write(typeof mix[element] + " "); } document.write("<br /> Type elements in numbers: "); for (element in numbers) { document.write(typeof numbers[element] + " "); } document.write("<br /> Type elements in booleans: "); for (element in booleans) { document.write(typeof booleans[element] + " "); } document.write("<br /> Type elements in mix2: "); for (element in mix2) { document.write(typeof mix2[element] + " "); } var sum = numbers[0] + numbers[1]; document.write("<br /> sum numbers = " + sum);
After executing the script, we will see the type of each element of each array and the sum of the first two digits from the array of numbers.
Type elements in mix: number string string string number number boolean Type elements in numbers: number number number number number Type elements in booleans: boolean boolean boolean Type elements in mix2: boolean number number sum numbers = 12
Conclusion
When you first touch the JavaScript language, a Java developer may have all of the above comments and questions. At my first acquaintance with JS, I experienced not the most cheerful emotions. Rather, it was like this: "What the ...?". Many differences and misunderstandings lie in the difference in the typifications of the two languages. I don't know why JS needs weak typing.
Perhaps there are benefits why this was done. If you know the answer, write in the comments.
Yes, there is a TypeScript, which seems to be typed, but in the end it will be translated into the same JS. Personally, I am not a supporter of weak typing, but my colleague, who recently tried JavaScript, for some reason was delighted with it. Perhaps this is a matter of taste. And what do you think is better weak or strong typing?