Hi there ๐๐ฟ
Welcome back,
In the last post of this series, I mentioned that I would be going through the vital thing learned from reading the book "You Don't Know JS" by kyle simpson. Of course, if you have not read the previous article here is a link to it ๐ค.
Okay, back to the conversation on these vital things which will be highlighted under many specific bullet points as follows:
Variables
Sure you can track a value as it changes over the runtime of your program via variables even after it has gone through quite a few series of operations. But the key thing to understand in JS is that it uses dynamic typing as opposed to static typing. Some call it weak typing, I might have done so on a few occasions but that was before I read this YDKJ ("You Don't Know JS"). But that is where the power lies, You are wondering how right? - The answer is responsibility. It is your duty to know what your program wants not what people send to your API or program. Focusing on your variable ensures that they work with the expected value type. Value types? what do you mean?
Value types are just your normal values in JS but each of these values has specific types in JS namely: number, string, boolean, object, function, and undefined.
Note : Null in Javascript is an Object type
so with all these, values should be checked against and any unwanted thing can be filtered out.
Constants
Constants of course are Constant i.e. they never change their value. You should only use
const
keywords for the values you know not values you will figure out at runtime. You can make the value of an object constant via theObject.freeze
method shown below since the keys of objects are not protected :const obj = { prop: 42 }; obj.prop = 34 ; // modifies the obj.prop to 34 Object.freeze(obj); obj.prop = 33; // Throws an error in strict mode console.log(obj.prop); // Expected output: 34
As an ending point on constants in JS, always use uppercase snake case for constant as a convention.
Blocks
The wonderful thing about blocks in JS is that you can carry out tasks that can be cleaned up immediately after the block ends. Also, this task might not affect your program in any way.
{ let someResource = getResource(); if (someResource.isValid()) { someResource.doSomething(); } else { console.error('Invalid resource'); } someResource.cleanup(); } // the code in the block runs and end in the block scope while other part of the program is not affected
Looking at the block scope, you can now attach the
if
,else
andelse if
the keyword as a precondition to forming a conditional statement.Conditionals
Sure as mentioned in the last bullet point, you only need to attach the
if
,else
andelse if
the keyword as a precondition to creating a conditional block. So I here the advised way of doing so :If you have more than 3 conditions
else if
use `switch case block insteadUse an if block if you can return early. Error first approach which is common in callbacks
Always write conditional in a way that encourages maintainability. It might not always work but if you can you should go for it.
These are the key takeaways for the second part of the discussion of this series.
Please share your thought about the article and if you find it interesting you can follow me on LinkedIn here