Uses of JavaScript
- 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.
- To run the code, JavaScript uses client machine processing power.
- With JavaScript, partial page updates are also possible without reloading an entire web form.
- For animation also, we can use JavaScript.
Disadvantages of JavaScript
- 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.
- 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.
- Code Always Visible: The biggest disadvantages is code always visible to everyone anyone can view JavaScript code.
- Stop Render: JavaScript single error can stop to render with the entire site. However, browsers are extremely tolerant of JavaScript errors.
- Disable JavaScript: If you disable JavaScript in the browser, the entire JavaScript code is not run.
- 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.
- console.log('Hello Sagar Jaybhay');
-
It is different from this below code
- console.Log('Hello Sagar Jaybhay');
The output of this code is -
- 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.
- var a="10";
- var b="20";
- c=parseInt(a)+parseInt(b);
- console.log(c)
parseFloat In JavaScript
By using this method you not lose decimal places so whenever needed use this parsefloat
- var a="10.2";
- var b="20.3";
- c=parseFloat(a)+parseFloat(b);
- 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.
- var a="sagar"
- console.log(isNaN(a))
- var b=10
- console.log(isNaN(b))