10 Interesting things you can do with a few lines of JavaScript


In this tutorial, let us discuss some fascinating things that a few lines of Javascript can do.

Almost every website uses JavaScript. JavaScript was born in 1995, and it is a programming language similar to other languages, but it runs faster because it doesn't have many predefined functions.

JavaScript can create programs, libraries, and scripts for websites or desktop applications. A lot of web developers are also good at writing JavaScript. Some JavaScript codes are fascinating if you observe how it behaves. Let us discuss what are these fascinating things in JavaScript.

Fascinating things about the semicolon

The end of a statement in a programming language contains a semicolon. But, unlike other languages, JavaScript uses a semicolon also to start the code. Do not follow this wrong approach.

;var x = 5

Interesting things about NaN

In JavaScript, anything that isn't a number is also a valid number. An example is "NaN" (Not a number).

Interesting things about Null

A null is an object that represents nothing. If you use the typeof function, you will get information about the null object.

Interesting things about the function

A function can run itself, just like you can do in other programming languages. When you call the below function, it will display "hi" on the screen

(function(){alert(‘hi’);})(); //gives ‘hi’ alert

Interesting things about script tag

Scripts can only have one source file. This file must be in the same directory as the script tag. You cannot include other files or code within the script tag. The following is not possible in JavaScript.

<script src="code.js">
   alert("Hi");
</script>

Fascinating things about adding two different types of values

Adding a string with a number gives a string. Adding a number with a string results in a number.

"100" + 2;//prints "1002"
100 + "2";//prints 102
alert("4"/"2"); //2
alert(2 + "3"); //23
alert("4" + 5); //45
alert("8" + 3 + 7); //837
alert(3 + 1 + "5"); // 45

Interesting things about IIFE,

In Immediately Invoked Function Expressions, we can expect different outcomes as follows.

//code runs as it is an assignment
var test = function () {

   //code
}();

// Syntax error as it is only a definition
function test() {

   //code
}();

Interesting things about undefined

Although "undefined" is a keyword in JavaScript, it doesn't have a specific meaning. Variables can have a special meaning called undefined, or they can be undefined. If a variable is undefined, you can't figure out what it is.

var vb;
alert(vb== undefined); //returns true
undefined = "value";
var vr;
alert(vr == undefined); //returns false

Interesting things about parenthesis

The parenthesis position will define different functions.

function a() {
   return
   {
      x: '1'
   }
}
function b() {
   return {
      x: '1'
   }
}
console.log(typeof a() === typeof b()); //returns false

Interesting things about numbers

Observe the output for the following.

alert(9999999999999999); //10000000000000000
let x= -0; // -0
let y= 0; // 0

This tutorial discussed some fascinating things that a few lines of JavaScript can do. You will be familiar with all these if you are a JavaScript developer. However, some of these are born bugs.

Without coding, we can do many cool things with JavaScript. JavaScript has had a lot of changes over the years. Make websites that move and make games that you control with your computer. You can also make phone apps and programs that run on your computer without software assistance.

Updated on: 17-Jan-2023

169 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements