Little things about Javascript

Javascript is a single-threaded or single-call stack programming language that can do one piece of code at a time. The call stack is a data structure that can see where the program is. If we return a function. we just pop off that from the stack.

Javascript has runtime concepts. Stack- function calls form a stack of frames, Heap(vast region of memory)- objects are allocated here, Queue- list of messages processed here. Event loop —

Run-to-completion: In this, each message is processed completely before any other message is processed.

Adding message: In this, messages are added anytime an event occurs and there is an event listener attached to it. The function setTimeout is called with two arguments: a message to add to the queue, and a time value.

There are many kinds of values-

  1. Primitive values: They are the numbers and strings for which we can not do anything in our code to affect it. They are not objects.

Primitive values can be -

undefined=> unintentionally missing value. so there is just only value that is undefined. This is a kind of troublemaker.

null=> intentionally missing value. so there is just only a null value. This is also a kind of troublemaker.

boolean(true, false)=>logical operations.

numbers=> math calculation.

string=>text.

Symbols​=>hide implementation details.

BigInts​=>math on big numbers.

2. Objects and functions: They are not primitive. We can change them from our code. When we run these, we can see that it is different from primitive one. There is a slight change in presenting them like an arrow sign before them. The object is for group-related data and code. The function is for referring to code.

Javascript expression: It is like a question’s answer that can be done with a single value. For example-

You are using a type of Javascript value. But you don’t know what type that is. Then you need to check it. So, To check the type there is a typeof operator that can tell you whether your type of value is string or numbers or any other type.

  • * To prevent ambiguity, it is better to use a round bracket( paren) in the console.
  • * Array, Date, and regular expressions are objects.

Error Handling: Life is full of errors, so do our codes. It can happen because of our fault, unexpected user input, an erroneous server response, etc. JavaScript has many built-in constructors for standard errors: Error, SyntaxError, ReferenceError, TypeError etc.

try…catch- this syntax construct can catch the error. Code will be in the try section and the error handling will be in the catch section. So, first, the code will try to execute. if there is no error, it will not go to the catch section. If there is an error, then it will stop executing and go to the catch section where an err variable will have an error object with details. But it works for runtime error or in a valid code. If the engine can not understand the code because of the parse-time error(syntactically wrong), try…catch can not catch the error.

For setTimeout function, try…catch must be inside the function. otherwise, it will not catch the error because the function itself is executed later when the engine has already left the try…catch construct.

Error object: JavaScript generates an object with the details. The object is then passed as an argument to catch. It has many properties-

name- this is the error name.

message- this the error message(the detail).

stack- current call stack: it is a string with information about the sequence of nested calls that led to the error. It is used for debugging purposes.

Example- JSON.parse which is used to decode data received over the network, from the server, or another source.

Throwing error: Sometimes our code is fine but it doesn’t have a required value. It will be an error. So, to unify error handling, we use the throw operator. It generates an error. Suppose a JSON.parse doesn’t have the property name. So, it will be an error. To catch this -

Apart from this error, if there is an error in the try section, it will not show the correct error. To know that we must rethrow the error. That’s called rethrowing error technique.

try…catch…finally- If it exists, it runs in all cases: after try, if there were no errors, after the catch, if there were errors. It is used when we start doing something and want to finalize it in any case of the outcome.

try…finally- it is also useful without a catch clause. We apply it when we don’t want to handle errors, but want to be sure that processes that we started are finalized.

Global catch- it can have the error message, URL of the script where the error happened, Line and column numbers where the error happened, Error object. It sends errors to the developers.

Woo-hoo! A lot of javascript information. Isn’t it?

Okay! Let’s talk about some basic things about coding — the style, comment, and many more…

Our coding style should be neat and clean so that anyone can understand it and it is correct in every aspect. So, we are going to point out some information that we can use in our day to day life-

  • There should be no space between function names and parentheses.
  • There should be no space between parameters and parentheses.
  • There should be a space between parameters.
  • To indicate inside a function there should be a curly brace on the same line after space.
  • There should be spaces around operators.
  • After ending a line or expression semicolon is the best use.
  • Inside any function or anything, there should be indentation(2 spaces).
  • There should be a space after for/if/while.
  • There should be an empty line between logical blocks.
  • There should be spaces around a nested call.
  • After the ‘if’ ending bracket, else should start with space, and else’s curly bracket should be in the same line with space.
  • Lines should not be very long, We should split them into multiple lines. There should not be more than nine lines of code without a vertical indentation.
  • There should be a space between arguments.
  • It's better not to use nesting code too many levels deep. It is better to use the continue directive to avoid extra nesting.
  • In the case of a function, we should write the code first, and then there comes the function. If the code goes first, then it becomes clear from the start.

Automated Linters: Many tools check the style of our code and change it accordingly.

  • JSLint— one of the first linters.
  • JSHint — more settings than JSLint.
  • ESLint— probably the newest one.

An example of the coding styles-

Comments in Code: Comments are really important to provide an overview of any part. For that code is more understandable. If we open our code that was written 6 years ago, We should understand it and use it effectively. For that comment is important. We should not put comments on how the code works or what it does.

Single line comment: //hi this is a single line comment

Multi-line comment: /* …….. */

Caching: It is a basic computer concept that provides efficiency through data availability.

Cross-browser checking: It is a practice of making sure that the websites and web apps we create work across an acceptable number of web browsers.

Initial planning => Development => Testing/discovery => Fixes/iteration

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
sazida lubna

Courteous and enthusiastic,have strong driving force for improving the performance,scalability, and reliability of development projects.