What type is generally used to declare the initial value of a variable in js?

problem description

What type is generally good for declaring the initial value of a variable in

js? Why
such as
var a / / initial value, this value may be followed by a string type or a numeric type, what should I use for initialization here? 0 or""

the environmental background of the problems and what methods you have tried

var a / / initial value, which may be followed by a string type or a numeric type. What should I use for initialization here? 0 or""

related codes

/ / Please paste the code text below (do not replace the code with pictures)

var a / / initial value, which may be followed by a string type or a numeric type. What should I use for initialization here? 0 or""

what result do you expect? What is the error message actually seen?

var a / / initial value, which may be followed by a string type or a numeric type. What should I use for initialization here? 0 or""

Mar.29,2021

js variable declaration does not necessarily need to be assigned, only const needs to be assigned when declaring, you can directly var an or let a. Assign a value when you need it. If you need a default value, it depends on the specific usage, usually an empty string "" or 0; if the variable does not need to be changed, it is recommended to use const.


depending on how you use this variable below, for numeric operations, you can give a 0 as the default value, for string concatenation, you can give a''as the default value, and if you want to use it as an array, use [] as the default value. If you assign values directly below, you can define variables above without default values.

Menu