Some Javascript topic You should know

Md Abdullah Mamun
4 min readMay 6, 2021

Primitive Data type

Javascript has two data types. One is a primitive value and other is Reference value.

List of Premitive value is string,number,boolean,null,undefined,symble(es6).On the other hand List of Reference value is object,array,function,date.

Primitive value is immutable.when primitive value create it does not change. That means if you change the primitive value you can not do it.

example :

var string = 'This is a string.';
string[1] = 'A'console.log(string) // 'This is a string.'

Let's look at another example

var a = ["shakib" , "tamim" , "rakib"]
var b = a
a.push("jakir")
console.log(a)//["shakib" , "tamim" , "rakib","jakir"]
console.log(b)//["shakib" , "tamim" , "rakib","jakir"]

Take a good look at the example. There is no change of a value.It creates different, a & b value same as each other.It's just creating another store when set var b = a. This is called primitive data value. But you can change data value when-

var a = [name: "shakib" , age: 18, height : 5.9]a[0].name= 'rakib'
console.log(a)//[name: "rakib" , age: 18, height : 5.9]

this method call Non-primitive value.

Hopefully, you understand. Thank you.

typeof ()

typeof() is a function where put the data value it will be found what kind of data. What is the value of data type string, array, number boolean or undefined, you can easily get it. typeof() method.
example:

var str = ‘hello’ ;
console.log(typeof(str)) // return string
example: var str = [‘hello’] ;
console.log(typeof(str)) // return array
example: var str = 1234 ;
console.log(typeof(str)) // return number

try-catch error handling

Suppose we have written twenty lines of codes. If Somehow code error in the fourth line, those line program will be stopped and the code will not work in the fourth to twenty lines. If we use error handling method the program easily runs if code error in any line.So try-catch has come to solve fro this problem.

example:

console.log(alert('hello world'))
console.log(alert('hello programmer'))
console.log(alert(x))
console.log(alert('have a nice day'))
console.log(alert('good luck'))

See the example above. The first two values code ok so the code will be run without problem . When it will be come on three line it will be failed because x value is not set. Now

try{console.log(alert('hello world'))
console.log(alert('hello programmer'))
console.log(alert(x))
console.log(alert('have a nice day'))
console.log(alert('good luck'))
}catch(err){console.log(err)}

we can see the example inside the try where first ,second code working ok when will come third line then catch function catch the error and it show the output.Code working fine.

Code Style

As a programmer, we should follow clean code style method. If you want to a good programmer you should practice how to code clean.code clean means your code looks like clean and follows the coding rules.Code will be human readable.

suppose your write a function and it has more parameter.You should not declare one line you should one by one line: Example

style(parameters,
aligned, // 5 spaces padding at the left
one,
after,
another ) {
// ... }

some example of coding style

if (   
id === 123 &&
moonPhase === 'Waning Gibbous' &&
zodiacSign === 'Libra' ){

letTheSorceryBegin();
}
for (
let i = 0; i < 10; i++){
if (cond) {
... // <- one more nesting level
}
}

you can see other example: https://javascript.info/coding-style

Comments

We don't just read our code, another developer also can read. So if we code with comments it is easy to read, It will be easier for another developer. And how this code works ,it will be easy to understand when another developer sees it.But we should avoid bad comment.

Bad comment example

// This code will do this thing (...) and that thing (...) 
// ...and who knows what else...
very;
complex; code;

Good comment example

/**  
* Returns x raised to the n-th power.
*
* @param {number} x The number to raise.
* @param {number} n The power, must be a natural number.
* @return {number} x raised to the n-th power.
*/
function pow(x, n) {
...
}

Arrow Function

Javascript has many functions. One method is a normal function and another is the Arrow function. I choice normal function to compare because it is easy to understand how to work the arrow function. Ok, let's follow example

//normal function
function yourName(name){
console.log('hello' , name)
}
yourName('nayem')
//arrow function
const yourName = (name) => {
console.log('hello' , name)
}
yourName('nayem')

look at the example above. Just arrow junction parameter the qual sign sits next to it . other is look like same .The point Look at is on the right side arrow sign of the parameter, that's why it is called arrow function.

Curly Brackets{}

Javascript projects curly brackets are written in the Egyptian style. If someone reads programs written in JavaScript, It should be able to read.If we use curly bracket in code our code will be clean and readable.Curly brackets are use function,conditional,objects ,destructive etc.Curly braces { } are special syntax in JSX. It is used to evaluate a JavaScript expression during compilation.

Object

Javascript object is a another data structure where canstore multiple data. It is a reference data type.It is data collection method.Here the data is arranged in a different ways. Suppose you have student data. Here the student is an object.we can store student data name,age,roll,address etc.example:

var student1 = {name :'shakib' , age: 18 , address: 'dhaka/bangladesh'}
console.log(student1)//{name: "shakib", age: 18, address: "dhaka/bangladesh"}

we can detect every student data with object mathod.

Function

The functionto to learn any programming language is one of its main thing . All languages have function.The function is nothing more than naming codeblock.If you call that name anywhere in the code later,the code block will be executed.But sometimes it is work different way on javascript.

function square(number) {
return number * number;
}
square(2)

--

--