Some random Untold topics of javascript
Javascript is one of the most used and effective programming language in the world.There is a saying that no one can ever end of learning javascript.You maybe learning js for so long time from several resources but there will be always something untouched.
Now let’s talk about some of the common important topics from javascript which are ignored most of the time .
1: Primitive values
JavaScript variables can hold many data types or primitive values: numbers, strings, objects, Array, Boolean, Functions and also Undefined, null. Array is a special kind of object and functions is also belong to objects.
//Example
var x = 12345;
console.log(typeof x) // number
2: typeof()
typeof
is a JavaScript keyword that will return the type of a variable when you call it. You can use this to validate function parameters or check if variables are defined
var x = 12345;
console.log(typeof x) // number
x = 'string';
console.log(typeof x) // string
x = { key: 'value' };
console.log(typeof x) // object
3: javascript Errors
A try / catch block is basically used to handle errors in JavaScript. We use this when we don’t want an error in your script to break your code.
The try
statement lets you test a block of code for errors.
The catch
statement lets you handle the error.
The throw
statement lets you create custom errors.
The finally
statement lets you execute code, after try and catch, regardless of the result
let data=prompt("myName")
try{
if(data==="") throw new Error("data is empty")
else alert(`Hi ${data} how do you do today`)
} catch(e){
alert(e)
} finally {
alert("We are done with try...catch")
}
4: Coding Style
Though coding or programming is an universal skill all the programmers have their own preferred programming language and coding style. Coding style actually means how you write and organize your code. learn more about coding style…
Best practice will be :-
- Give cleaner code
- always go for local variable
- Local variables must be declared with the
var
keyword or thelet
keyword - Make it easier to avoid unwanted (implied) global variables
- Less use of unwanted re-declarations
// Declare at the beginning
var firstName, lastName, price, discount, fullPrice;// Use later
firstName = “Mike”;
lastName = “Tyson”;price = 90;
discount = 0.10;fullPrice = price — discount;
5: Comments
Comments are one of the most important thing while programming. We use comments to add some description, mute any code etc.There are 2 types of comments in Js .
- Single line comments start with
//
. Any text between//
and the end of the line will be ignored by JavaScript - Multi-line comments start with
/*
and end with*/
. Any text between/*
and*/
will be ignored by JavaScript.
var x = 5; // Declare x, give it the value of 5/*
A new Variable
*/
6 JavaScript Variables
JavaScript variables are containers that stores data values
There are 3 types of Variables
1- var
var myName = khan;2- let
let myName = khan;3 const
const myName = khan;
7 Date Formats
There are generally 3 types of JavaScript date input formats:
ISO Date"2015-03-25" (The International Standard)Short Date"03/25/2021"Long Date "Mar 25 2014" or "25 Mar 2014"
8 Logical Operator
Logical operators are used to determine the logic between variables or values.
&& and(x < 10 && y > 1) is true || or (x == 5 || y == 5) is false! not!(x == y) is true
9 JavaScript Errors
The try
statement lets you test a block of code for errors.
The catch
statement lets you handle the error.
The throw
statement lets you create custom errors.
The finally
statement lets you execute code, after try and catch, regardless of the result.
10 JavaScript JSON
JSON is a format for storing and transporting data. JSON is often used when data is sent from a server to a web page.
What is JSON?
- JSON stands for JavaScript Object Notation
- JSON is a lightweight data interchange format
- JSON is language independent *
- JSON is “self-describing” and easy to understand