Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is declare var in Node.js?

in this nodejs code,

declare var process: NodeJS.Process;
declare var global: NodeJS.Global;
declare var console: Console; 
declare var __filename: string;
declare var __dirname: string;

that...

What's the difference between 'declare var' and 'var'?

When I look up on the googling, I get the word runtime.

wiki says runtime is an operation while a computer program is running....

but i can't understand.

and line 1, what does it mean by ":" after "process" and then "NodeJS.Process"?

Is that mean "process" is equal "NodeJS.Process"?

also line 4, what does it mean by ":" after "__filename" and then "string"?

Is that mean "__filename" is equal "string"?

thanks you.

like image 758
ONION Avatar asked Apr 10 '18 05:04

ONION


People also ask

What is declare VAR in JS?

You declare a JavaScript variable with the var or the let keyword: var carName; or: let carName; After the declaration, the variable has no value (technically it is undefined ).

Why VAR is used in JavaScript?

The var keyword is used to declare variables in JavaScript. Before you use a variable in a JavaScript program, you must declare it. Variables are declared with the var keyword as follows. Storing a value in a variable is called variable initialization.

What is var and function?

VAR assumes that its arguments are a sample of the population. If your data represents the entire population, then compute the variance by using VARP. Arguments can either be numbers or names, arrays, or references that contain numbers.

What is var syntax?

The VAR keyword introduces variables in an expression. The syntax after VAR defines a variable, which can be consumed in following VAR statements or within the mandatory RETURN statement following the declaration of one or more variables.


1 Answers

When you use:

var process: NodeJS.Process;

You are creating a variable named process (with no value defined) and telling the TypeScript compiler to enforce the NodeJS.Process type for assignments.

When you add declare:

declare var process: NodeJS.Process;

You are telling the TypeScript compiler that there is already a variable named process with the type NodeJS.Process. This is useful when you have variables introduced by sources that the compiler is not be aware of.

See Declaration Files in the TypeScript handbook.

like image 72
Fenton Avatar answered Oct 01 '22 06:10

Fenton