When people with Java experience learn JavaScript for the first time, By learning only the differences between Java and JavaScript syntax Do not read thick textbooks at all, in a short time Let's master the minimum necessary! Is the concept
JavaScript is a scripting language that is basically responsible for mechanical processing of web pages and dynamic content value changes.
Unlike Java, execution processing is OK if you keep in mind that ** each Web browser ** analyzes and executes the syntax (interpreter method).
Only specific differences will be introduced. Thorough implementation of the following syntax enables programming that is not much different from Java. However, there are various project policies at the actual site, so please respond flexibly!
JavaScript even if there is a syntax error in part of the file (compile error) It wraps it up with kindness to some extent
If you have a steel spirit that repels that tenderness, put a magic spell at the beginning of the file.
magic.js
"use strict";
Like Java, types can be roughly divided into primitive types and object types, It is not possible to declare variables separately for the description.
kataSample.java
satic final String name = "Mihashi";
int age = 14;
List<String> belongings = new ArrayList<>();
kataSample.js
const name = "Mihashi";
let age = 14;
//The array will be described later.
let belongings = ["rice ball","Tantrum","bat"];
** ⭐︎ points ** --All variable declarations are made with "let" (* 1) --There is no access modifier (* 2) --Use "const" as a constant
The comparison is straightforward as follows. When you use the NG pattern, type conversion is done internally, so In some cases, the comparison between a character string and a numerical value is also a true judgment.
compare.js
console.log("1" === "1");
console.log("1" === 1);
console.log(1 >== 0 && "Ito" === "Mihashi");
//NG pattern
console.log("1" == 1);
true
false
false
true
For object type, use equals () method as before
Supplement from comments (from @sdkei)
JavaScript == is like a fairly flexible implementation of Java's equals method. JavaScript === is the same as Java ==.
It would be nice if you could write the for statement as usual, The extended for statement of the array (List) is described as follows.
forSample.js
let sampleList = ["Delicious banana","Rotten oranges"]
for(let item of sampleList){
console.log(item);
}
The alternatives to HashMap and ArrayList in Java are as follows
mapSample.js
//Map generation
let map = new Map();
//Add element
//* Note: set, not put!
map.set("key1", "apple");
//Get element(As usual)
map.get("key1");
listSample.js
//List generation(Virtually just an array)
let list = new Array();
//Add element
//* Note: push, not add!
list.push("apple");
//Get element(How to access the array)
list[0];
use the function function
kansuSample.js
function sampleFanc(name){
console.log(name);
}
sampleFanc("Imai");
In the JavaScript world, there is the concept of undefined. Variables that are not generated in Java (do not exist in memory) are treated as null, Think of it as undefined in JavaScript.
Note that null and undefined are treated differently, so When "===" is compared, it is judged as false.
Since the contents introduced above are really narrowed down to the minimum necessary, an error will occur if you do various things (irresponsible) Therefore, it is important to have a method to identify the cause of what error is occurring.
specificErr.js
try{
let name = "Value that seems to be a bug";
//Log if uneasy
console.log(e);
}catch(e){
//try / catch possible
console.log(e.massage);
}
In addition, by using the functions for developers of each Web browser standard, more accurate identification can be performed.
It can be said that the above contents have solidified the basics of JavaScript syntax. After that, if you learn JavaScript-specific libraries, event handling, framework methods, etc., you should be able to do what you want.
Recommended Posts