JavaScript any way to declare strict type variables [duplicate] - javascript

This question already has answers here:
Typesafe Javascript
(8 answers)
Closed 2 years ago.
Hy I am a new to JavaScript. I have done programming with Java. In Java we declares variables with proper type. string, int, char etc. Is there a way to declare variable in JavaScript having proper type instead of let, var etc.

While using javascript stack (javascript, typescript) you are going to use let/const but you can either strictly define the type like:
let a: number = 5;
or leave it to javascript engine
let b = 5;

Javascript is a dynamically typed language, the type is assigned by the javascript itself, it has some ways of assigning types to variables, but none natively, they all need some framework.
The most famous way is typescript which is a superset of javascript, other way is flow.js.
If you want only check a variable type you can use typeof or === operator. e.g: == check only value, 1 == "1" is true, but === check value and type, 1 === "1" is false.
If you're new to JS I personally recommend focus in learn JavaScript and in future study other frameworks such as Typescript.
useful study links
https://www.w3schools.com/js/
https://developer.mozilla.org/en-US/docs/Web/JavaScript

Related

What does the "var" before a JavaScript array initialization refer to, the elements of the list or the array itself? [duplicate]

This question already has answers here:
What does "var" do in JavaScript? Why is it sometimes part of an assignment? [duplicate]
(3 answers)
Declaring javascript variables as specific types [closed]
(1 answer)
Closed 4 years ago.
I was taught to use "let" to initialize arrays in JavaScript, but I've recently discovered that "var" can be used as well.
var evenNumbers=[2,4,6,8];
I know that it's possible to initialize a function in JavaScript using var, as in
var hello=function(){};
so naturally, I assumed that the "var" being used to initialize the variable refers to the name of the array, in this case evenNumbers.
However I also recently learned that to initialize arrays in C, which I think of as the grandfather of Java-type languages, the type of variable used in the array is used to initialize the array call.
int evenNumbers[]={2,4,6,8};
Obviously in this case, int refers to the elements of the list, since an array is not an int.
I therefore assumed that var before an array call in JavaScript refers to the elements of the list. I tried to test it by applying the wrong strong type to a new JavaScript variable, like
int newYearsResolutions=["Stop procrastinating"];
Which gives me an unexpected identifier, but that's not too helpful since an array is not an int nor is "Stop procrastinating" an int. I then tried
int evenNumbers=[2,4];
and this gives me the same error, leading me back to my original conclusion that the var being named here is "evenNumbers" and not the ints 2 and 4, but I still feel like I might be missing something.
So, var evenNumbers=[2,4] appears to name the evenNumbers variable and not the elements of the array. I just want to double-check that that's the case.
JavaScript is not a typed language. int isn't a reserved word, and thus is not doing anything. var, let, const are all ways to assign variables - regardless of type.

TypeScript cast using "as" [duplicate]

This question already has answers here:
Casting a number to a string in TypeScript
(8 answers)
Closed 4 years ago.
I was trying to cast a type any to boolean. So I simply did this:
let a = (<any>myType) as boolean;
But a was not a boolean it just contains myType value.
However When I try this:
let b = Boolean(myType);
b returns a boolean which is false.
Am I missing something about "as" casting?
Which one is correct to cast to boolean?
Casting — or more properly, type assertion — is not conversion/coercion. It has no runtime effect in TypeScript.¹ It's just the process of telling TypeScript that a variable or property is of the type you're casting it to asserting. In your first example, a gets the exact same value that myType has in it, it's just that the TypeScript compiler then believes a contains a boolean. That type information disappears before the code is run, since TypeScript is compiled to JavaScript without decorating the code to convey type information, and JavaScript variables are loosely typed.
To actually convert the value, you'd use conversion (such as your Boolean(myType) example) instead.
¹ Don't over-generalize this to other languages, such as Java or C#. This is one of the reasons TypeScript calls the process type assertion rather than casting. Casting may or may not be conversion in those other languages depending on the cast; and separately casting in those other languages can have an effect on runtime behavior.

Does defining a frequently used string as a variable improve performance? [duplicate]

This question already has answers here:
Do common JavaScript implementations use string interning?
(3 answers)
Closed 7 years ago.
In some languages, frequently used strings are defined as variables/constants, which are called instead of literal strings. Is this the same with JavaScript? In particular, I have frequent use of the string 'none'. Instead of writing the literal 'none' everywhere in the code, would it improve performance if I define:
var none = 'none';
and use none everywhere in the code? Or, is there a way to intern a literal string expression so that it is evaluated only once?
Literal strings are automatically interned by most Javascript compilers. So var a = 'hello' and var b = 'hello' will likely already be pointing at the same copy of the 'hello' string in memory, no need for further optimization on your part.
The only way to make sure different string objects are created for the same string value is by defining each one via the String global object, i.e.:
var a = new String('hello');
var b = new String('hello');

Inverse comparison/equals arguments [duplicate]

This question already has answers here:
Why Constant first in an if condition [duplicate]
(2 answers)
Checking for null - what order? [duplicate]
(8 answers)
Closed 8 years ago.
I saw many times in open source projects that folks write something like that:
if("" !== foo) {
// ...
}
Why on earth do they do that? I mean you are checking if foo's value is empty string or not. I understand that "" !== foo and foo !== "" means exactly the same. But what's the reason to write tricky and less obvious code?
I'm personally not a fan of this style, either; however, the style stems from this:
if (x = "") { // **oops**, meant to do "==" but ended up assigning to "x"
// ...
}
The "if" statement here will cause an unintended side-effect. There are a number of languages in which side effects and implicit conversions to boolean make it easy to shoot oneself in the foot this way (JavaScript and C++ come to mind). To avoid this, some have the convention of inverting the ordering, since assigning to a constant will produce a more immediate and obvious error. I'm personally not a fan of this, because it makes the code read less like English ("if x equals the empty string" reads more naturally than "if empty string equals x"), and better tooling (linters, source code analyzers, etc.) can catch these just as effectively.

Can you set an undefined variable as a function parameter? [duplicate]

This question already has answers here:
Undefined variable as function argument javascript
(2 answers)
Closed 8 years ago.
I am building a function to match types of variables. It will be an alternative to typeof <var> === "something".
My function call looks like this : is("some text")["string"]. It returns true, or is([])["element"]. Now it returns false, But I have an issue with it.
For example, if I try to send an undefined variable like "undefVar" to a function I am expecting something like this: is(undefVar)["undefined"], but I get an error which says that "undefVar" is not defined.
Can I somehow make my function work using undefined variables? Is this possible at all?
p.s: I have to use this function a lot so it seems (for me) that it would be better to use something like this : is(var)[type] as opposed to typeof var === type.
typeof is special on nonexistent variables. You cannot mimic it with a function.
No, you cannot blindly pass undefined variables to your function from the call site and globally change the behavior of the JS engine to accomodate this.
typeof is a built-in operator and is not bound by the rules of "common" functions, which is why it can do things that custom functions cannot.
Finally, I strongly disagree that it would be practically preferable to use an alternative syntax even if that were possible.

Categories