Home  >  Article  >  Web Front-end  >  Can JavaScript write a backend?

Can JavaScript write a backend?

WBOY
WBOYOriginal
2022-06-15 16:20:075880browse

JavaScript can write back-end; JavaScript can use "Node.js" to achieve back-end development. "Node.js" is a development platform that allows JavaScript to run on the server and is an event-driven "I/O "Server-side JavaScript environment, "Node.js" can be understood as JavaScript running on the server side, so JavaScript can be written on the back end.

Can JavaScript write a backend?

The operating environment of this tutorial: Windows 10 system, JavaScript version 1.8.5, Dell G3 computer.

Can JavaScript write the backend?

Can JavaScript write the backend. JavaScript can achieve back-end development through Node.js technology. Node.js is a development platform that allows JavaScript to run on the server side.

Node.js is a development platform that allows JavaScript to run on the server side. It makes JavaScript a scripting language on par with server-side languages ​​such as PHP, Python, Perl, and Ruby.

First of all, it is important to understand that Node is not a web server. It doesn't do anything by itself. It doesn't work like Apache. If you want it to be an HTTP server, you have to write it yourself with the help of its built-in libraries. Node.js is just another way to execute code on the computer. It is a simple JavaScript Runtime.

Simply put, Node.js is JavaScript running on the server.

Node.js is a platform built on the Chrome JavaScript runtime.

Node.js is an event-driven I/O server-side JavaScript environment based on Google's V8 engine. The V8 engine executes Javascript very quickly and has very good performance.

Some problems that arise when writing back-end JavaScript:

1. The problem of floating point data losing precision:

Floating point types are divided into Single-precision floating-point type (float) and double-precision floating-point type (double) are described in detail in strongly typed languages ​​(C, JAVA), and are collectively called numerical types (Number) in JS. Interested readers can learn relevant knowledge. This article continues to return to the above questions.

First of all, let’s take a look at the performance of the loss of accuracy problem. The source code is as follows:

console.log(0.1+0.2);    //0.30000000000000004

The result should be 0.3, but the running result is confusing. This is absolutely not allowed in back-end development. This problem does not only appear in JS. After testing, this problem exists in JAVA and PHP, but does not exist in C. Why haven’t I learned any other languages? I don’t know why!

After talking about the problem, let’s talk about the solution. The source code is as follows:

const floatAdd = (arg1, arg2) => {
    let r1,r2,m;
    try {
    r1 = arg1.toString().split(".")[1].length;
    } catch(e) {
    r1 = 0;
    }
    try {
    r2 = arg2.toString().split(".")[1].length;
    } catch(e) {
    r2 = 0;
    }
    m  =Math.pow(10,Math.max(r1,r2));
    return (arg1*m+arg2*m)/m;
};
 
console.log(floatAdd(0.1,0.2));    // 0.3

2. BUG in toFixed() rounding:

toFixed() method uses fixed-point representation method to format a numeric value. To put it simply, it is the process of rounding the data, and the fixed point represents the number of decimal places to be retained.

Example:

let a=1.115;
console.log(a.toFixed(2))    //1.11

[Related recommendations: javascript video tutorial, web front-end]

The above is the detailed content of Can JavaScript write a backend?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn