Revisiting JavaScript

Uses of JavaScript

  1. Client-side validation: by using this, the application gets much more responsive and we have no need for the round trip on the server. For example- If we are doing Form-Validation by using client-side validation, we reduce the round trips on a server which means we reduce the load on the server.
  2. To run the code, JavaScript uses client machine processing power.
  3. With JavaScript, partial page updates are also possible without reloading an entire web form.
  4. For animation also, we can use JavaScript.

Disadvantages of JavaScript

  1. Security: JavaScript runs on the client machine so hackers may use JavaScript to do a variety of things like tracking your browser history, stealing your password.
  2. Browser Compatibility: For the same piece of JavaScript code may be treated differently by a different browser. Example: innerText is supported by Chrome and IE but not Firefox.
  3. Code Always Visible: The biggest disadvantages is code always visible to everyone anyone can view JavaScript code.
  4. Stop Render: JavaScript single error can stop to render with the entire site. However, browsers are extremely tolerant of JavaScript errors.
  5. Disable JavaScript: If you disable JavaScript in the browser, the entire JavaScript code is not run.
  6. File Download: JavaScript file is download on the client machine so anyone can read the code and reuse it.

Where should we put <script></script> tag in Html?

The script tag should be placed in the body or head section of the HTML page. In general, <script> tag can be placed in the body of HTML.
If you placed <script> tag in the head section of the Html page, then until your controls are not loaded, your code might not work.
Also, another reason is that if you refer to an external JavaScript file and you refer this in the head section, the web browser does not start the parsing of web-page until the external file is completely downloaded.
It is better to place the <script> tag near closing body tag. 

Is JavaScript case sensitive language?

Yes, JavaScript is a case sensitive language. Variable names, keywords, methods and object properties, and event handlers these all are case sensitive.
  1. console.log('Hello Sagar Jaybhay');  
  2. //output  Hello Sagar Jaybhay  
It is different from this below code
  1. console.Log('Hello Sagar Jaybhay');  
 The output of this code is -
  1. TypeError: console.Log is not a function  

Different methods to convert string to number in JavaScript 

parseInt in JavaScript
This is inbuilt method in JavaScript by which we can convert string to number.
  1. var a="10";  
  2. var b="20";  
  3. c=parseInt(a)+parseInt(b);  
  4. console.log(c)  
parseFloat In JavaScript
By using this method you not lose decimal places so whenever needed use this parsefloat
  1. var a="10.2";  
  2. var b="20.3";  
  3. c=parseFloat(a)+parseFloat(b);  
  4. console.log(c)  
NaN In JavaScript
In JavaScript, NaN stands for Not-a-Number. In JavaScript, we have isNan() function which determines whether a value is an illegal number or not. It will return value true if string is not valid number else it will return false.
  1. var a="sagar"  
  2. console.log(isNaN(a)) //true  
  3. var b=10  
  4. console.log(isNaN(b)) //false