[For beginners] About the JavaScript syntax explained by Java Gold

concept

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

What is JavaScript

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).

Syntax (difference only)

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!

strict mode (strict mode)

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";

About the type

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

Variable comparison

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

JavaScript == is like a fairly flexible implementation of Java's equals method. JavaScript === is the same as Java ==.

Extended for statement

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);
}

Map and List

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];

function

use the function function

kansuSample.js


function sampleFanc(name){
   console.log(name);
}

sampleFanc("Imai");

null and undefined

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.

How to find the error

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.

  1. Log
  2. Supplement the error

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.

Summary

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

[For beginners] About the JavaScript syntax explained by Java Gold
[For Java beginners] About exception handling
About the procedure for java to work
[For beginners] Quickly understand the basics of Java 8 Lambda
Credentials referenced by the AWS SDK for Java by default
Think about the JAVA = JAVAscript problem (needed in the future)
Java debug execution [for Java beginners]
Understand the Singleton pattern by comparing Java and JavaScript code
Object-oriented summary by beginners (Java)
Traps brought about by the default implementation of the Java 8 interface
Java for beginners, data hiding
A memorandum to reach the itchy place for Java Gold
Understand the Iterator pattern by comparing Java and JavaScript code
Java application for beginners: stream
About the relationship between the Java String equality operator (==) and initialization. Beginners
[For beginners] Personally thinking about the cause of the error-the idea of ​​the solution
[For beginners] Summary of java constructor
Rock-paper-scissors game for beginners in Java
Java for beginners, expressions and operators 1
[For beginners] Run Selenium in Java
Java for beginners, expressions and operators 2
About the current development environment (Java 8)
Object-oriented course for beginners sent by beginners
About Eclipse environment (Java, Liberty, JavaScript)
The road from JavaScript to Java
Classes and instances Java for beginners
[Java Siler] About type inference by var
Guess about the 2017 Java Persistence Framework (3) Reladomo
Introduction to java for the first time # 2
Learn Java with "So What" [For beginners]
[Ruby on Rails] About bundler (for beginners)
About the new Java release model @ Seki Java (2018/07/20)
[For beginners] Difference between Java and Kotlin
Java syntax changes being considered by Amber
Java Programming Style Guide for the Java 11 Era
Learning for the first time java [Introduction]
The story received by Java SE11 silver
Java vs. JavaScript: What ’s the Difference?