7 interesting tips You need to know About JavaScript ?
Interesting things about JavaScript that you may not have known!
Have you ever thought about the Bermuda Triangle? Or to the Hanging Gardens in Babylon? What about Artemis Shrine? There are many wonders in the world, for which there are logical reasons, and for others, no logical reason has been stated so far. There are wonders in the world of computers and programming languages. Maybe they are not wonders, but they are issues that have not attracted our attention so far. This article will discuss seven interesting points about JavaScript and wonders or things that we have not paid attention to in this language. So join us.
We have created a file called oddity.js in which we check step by step what we say below. We suggest you create such a file and follow our step by steps.
1. Is null an object?
We all know null as an object derived from the Object class and has no value inside it. Also, whenever we want a variable from one step to another to have no value in another program, we set that variable to null. But is that the case? Let’s look at this.
To do this, enter the following code snippet in the Editor:
alert ( typeof null );
As a result of the above code snippet, the value true is returned to the output, and null is recognized as an object. The key type is used to check the data type of a variable.
Read more about typeof: typeof is a registered word or so-called keyword that we can use to understand the data type. For example, the following code snippet defines a bool variable with a boolean data type:
var bool = true ;
As a result, the output of the following code snippet is boolean:
alert (typeof bool );
If you would like to read more about typeof, we suggest reading this article.
Now we want to check the problem with an instance of:
alert ( null instanceof Object );
The output of the above command is surprisingly false !! And this means that null is not taken from the Object class !! What happened here? We are also ignorant because of you !!
Read more about an instance of: an instance is a keyword and checks if an object is taken from a class. If the object is taken from the class, the answer is true. Otherwise, the answer is false. Consider the following example:
function Car ( name, color, year ) {
this . name = name;
this . color = color;
this . year = year;
}
var BMW = new Car ( 'BMW' , 'blue' , '2000' );
document . write ( BMW instanceof Car );
In the code snippet above, we defined the Car class and then took the BMW object from this class. In the next line, we use an instance to check if the BMW object is derived from the Car class. The output of this check is true.
If you want to know more about an instance, we suggest you read this article.
2- NaN is not even equal to itself!
NaN stands for Not a Number. We want to use typeof to find out what NaN data types are:
alert ( typeof Na N);
The output of the above code snippet is Number, which means that NaN is a number. As we know, every number is equal to itself. Now let’s see if NaN is equal to itself:
alert (NaN === NaN);
The output of the above code snippet is false !! And this is another contradiction in JavaScript !!
In addition, the values of NaN + and NaN- are equal to NaN itself. Note the following code snippet:
var pNaN = + NaN;
var negNaN = -NaN; alert (pNaN); alert (negNaN);
The output of the above code snippet is NaN. Both values are equal to NaN, so + NaN and -NaN must also be equal. Let’s look at this now. Note the following code snippet:
var pNaN = + NaN;
var negNaN = -NaN;
alert (pNaN == negNaN);
The output of the above code snippet is false, which means that although NaN + and NaN- are both equal in value to NaN, they are not equal. It is as if we have defined the value of the two variables a and b as 5, but these two are not equal!
3- How many zeros can be defined in JavaScript?
In the continuation of article 7 interesting points about JavaScript, we will examine how many zeros we have in this language.
In mathematics, it can be proved that zero is zero. Also, there has never been talking of zero being positive or negative in mathematics. Now we want to know what this is like in JavaScript. Do we not have positive and negative zeros in JavaScript?
In JavaScript, we can define two zeros. One positive zero and one negative zero. It is interesting to know that each of these zeros equals zero. Consider the following example:
var pZero = + 0 ;
var negZero = - 0 ;
alert (pZero);
alert (negZero);
The output of the above code snippet is both zero. Now let’s check if the two are equal. Or will the result we get in NaN be repeated? To do this, enter the following code snippet in the Editor:
alert (pZero === negZero);
The output of the above code snippet is true, which means that both positive zeros and negative zeros are equal. Why do you think JavaScript has zero positives and zero negatives? Although they are both equal!
4- Scope of implementation of variables
Scope of Variables is the range in which a variable is defined and accessible. In JavaScript, variables have two types of definition domains:
Global: Variables whose scope of execution is Global are available anywhere and can be used anywhere in the program.
Local: Variables whose scope of execution is Local or local are often defined within a function, and their range of access is limited to that function.
Consider the following example:
var animal = 'dog' ;
function getAnimal ( adjective ) {
alert (adjective + '' + animal);
}
getAnimal ( 'lovely' );
In the above command, the animal variable is defined as Global. Therefore, its access range is not limited, and it can be used in the get animal () function. As a result, the output of the above code snippet is “lovely dog.”
All the variables and functions that we define are, in fact, properties and methods of the window class. As a result, they can also use this keyword inside the function. (For example, in the above code snippet, write this. animal instead of animal) Now we want to make sure that when we use this, instead of pointing to the window class, it points to the class we have defined and receives the value of the animal variable from it. To do this, use the Call () function as follows:
var animal = 'dog' ;
function getAnimal ( adjective ) { alert (adjective + '' + this . animal ); };
var myObj = { animal : 'camel' };
getAnimal. call (myObj, 'lovely' );
The code snippet above the function receives the animal variable from the myObj class. As a result, since the value of the animal variable in myObj is equal to camel, the output of the above code snippet is a lovely camel.
Also, in the above code snippet, the apply () function can be used instead of the Call () function. In this case, the code is written as follows:
var animal = 'dog' ;
function getAnimal ( adjective ) {
alert (adjective + '' + this . animal );
}
var myObj = { animal : 'camel' };
getAnimal. apply (myObj, [ 'lovely' ]);
5- New function definition method
To use functions in JavaScript, we first define the function and then call it, as in the following example:
function sum ( x ) {
return x + x;
}
alert (sum ( 5 )); // output 10
We want to define the function in a new way and use the function in a new way. Note the following code snippet:
var sum = new Function ( 'x' , 'return x + x' ) ;
In the code snippet above, we defined an object called sum from the Function class. We also give the values x and return x + x as arguments to the Function. And in the second line, to see the contents of the sum, we converted it to a string using the too.String () function. The output of the above code snippet is as follows:
According to the output, in the above command using new Function, we create a function called anonymous that the argument of this function is the first argument of the function that we created with new Function, and also the content of the defined function is equal to the second argument of the above function.
Now look at the following code snippet:
alert ( sum ( 4 ));
The output of the above code snippet is equal to 8. The object we got from the Function class can be used like this. In other words, the definition object name is essentially the same as the function name that we define in the usual way.
6- Functions that can call themselves!
There are functions in JavaScript that can call themselves. Normally when we define a function, the function does not execute until we call that function in a part of the program. The execution of a function depends on our calling and use of that function. Note the following code snippet:
( function () {
document . write ( "ded9" );
}) ();
The above code snippet is a self-executing function. In JavaScript, these functions are used for many purposes. It is enough to put the function in parentheses and finally add (). To learn more about these functions, we suggest you read this article.
7. Encrypt your code in JavaScript
In this part of Article 7, Interesting Tips about JavaScript, we are going to introduce you to two ways to encrypt your code in JavaScript:
1- Using the CryptoJS library
There is a library in JavaScript called CryptoJS written by Jeff. Mott. OR. Using this library, you can encrypt your code with several algorithms, including MD5, SHA-1, SHA-2, SHA-3, HMAC, PBKDF2, AES, TripleDES, Rabbit, RC4.
To learn more about this library, we recommend reading this article.
2- With this tool, encrypt your entire code in a fraction of a second
The javascript obfuscator website allows you to encrypt code written in JavaScript easily.
To do this, we enter the site:
Choose one of the two options, Download App or Online Obfuscator:
If we select Download App, we will enter the following page:
We register on the site and enter the following page. Then click on the API Keys as shown:
Now we download one of the apps. Then we enter this API Key into the software to be ready to work.
If we select Online Obfuscator, we will enter the following page:
In the image above, there is a Choose File option, with which we can load our JavaScript file or enter the code into the site as an example. Now click on the Obfuscator button and get the encrypted file, as shown below:
Conclusion:
Computers and all programming languages have strange and undiscovered things. Or maybe things we haven’t paid attention to yet. In this article, we mentioned seven interesting points about JavaScript. What did you encounter in JavaScript that was interesting to you?