Types in JavaScript
In simple terms, a type group supports equal values on which common activities can be performed. JavaScript has six primitives types:
- string
- Number
- undefined
- Null
- boolean
- symbol
There is also a compound type or object. Interestingly, the primitive types are immutable and don’t have properties.
You can easily check the type in your browser console and it will return you the desired answer. Just you have to use the “type()” method as well.
Like:
- console.log(typeof(2)) and it will return “Number”
- console.log(typeof(“Rahat”)) and it will return you “String”
- console.log( typeof x => x*4) and it will return you an error because when its about function we need to put all elements inside a bracket like below:
console.log(typeof( x=> x*4)) will return you “function”
Expressions
In JavaScript Expression means usually in our real-life whenever we try to express something is called expression. Real-life expressions and JavaScript expressions are almost the same.
Like in JavaScript if you ask something like what is the answer of (2+2)? And it will answer “4”. In real life we say (2+2) is equal to 4 or some say like “evaluate to 4”. Both mean the same but the way is different to express. And that’s called expression.
If you write in browser console that:
console.log(2+2); it will return you 4
Expressions always result in a single value.
Try…Catch
Try…catch is a syntax that helps us to catch errors without getting errors executing a code. No matter how good we are at programming, sometimes our script has errors and it stops working immediately when there is any mistake or error. At that moment try…catch works well.
Try catch should be write like below:
Try{
//////////////// code
}
Catch(error){
//// console.log(error)
}
Here is how it works; First the code inside the try will be executed and if there’s no error then the code inside the catch will be ignored. This means if there’s no error in try then the catch will be ignored.
And if there’s an error in try then catch will catch or capture the error and will show us via console.log(error).
So an error inside the try doesn’t kill the whole code rather it shows the error output and code runs as well.
Coding Style
We should write our code as clean and easy to read as possible. No one likes to read complex. And coding readable and clean is an art in programming.
Though nowadays most programmers prefer to code in any text editor or code editor to write code and most of the time some extensions help us to write code clean and easily readable.
So nowadays there’s no need to be worried about writing your code clean if you use one of these extensions. It will automatically format your code and that’s the best code formatting and too easy to read.
Comments
An important sign of a good developer comment: their presence and even their absence.
Good comments allow us to maintain the code well.
Comment this:
- The overall architecture, high-level view.
- Function usage.
- Important solutions, especially when not immediately obvious.
Avoid comments:
- That tells “how code works” and “what it does”.
- Put them in only if it’s impossible to make the code so simple and self-descriptive that it doesn’t require them.
Cross Browser Testing:
Cross-browser testing means we all web developers should test our websites or projects in various browsers. Because there are so many browsers and I don’t know which one my client is using my website or services.
And that’s why we all developers should test in various browsers as much as possible. Though some issues occur when we use the same website on various browsers and most of the time it happens because of the different features and technologies in these browsers that make the developers’ life hell.