Sign in
Log inSign up

Basics of JS (I)

Prathamesh Parab
·May 31, 2021·

5 min read

Basics of JS (I)

JS is similar to C++, Java, and Python. To transform this JS code into machine-readable code, some tools are required. As a result, JS creates a compiler that can turn your JS code into executable code.

It is a fallacy that JS executes in the browser; nevertheless, much like any other programming language, JS has a compiler that performs a lot of intermediate work before putting the code in an executable format in the browser. One of the JS Engines is V8, which is similar to SpiderMonkey. NodeJS is a JSEngines implementation that translates StandAlone JS code. It is not necessary to embed JS into HTML and reload each time.

JS comes up with a method to do that by generating less code.

JS engines also have optimization functions that make the code even smaller. JSLint is a tool used by js developers to find bugs and issues that can be avoided using available optimization methods so that code size decreases and performance increases resulting in smooth experience when using your JSEngine code without any errors or memory leaks.

Which JS Version Should I Use?

The world of JavaScript can be perplexing. ECMAScript, JavaScript, ES5, and ES2015 are discussed (which is the same as ES6 by the way).

Until ES5, things were very much the same, with the exception of colour changes and other DOM stuff. When ES6 was introduced, the tables were turned.

ECMAScript is a language specification, and Javascript is a programming language that implements it. When it comes to ECMAScript versions, there is currently no naming system. As a result, ES10 is also known as ECMAScript2019 and ES2019.

Furthermore, the most recent significant modification to the language was ES6, which was released in 2015, and is frequently referred to as ES2015. However, the most current version of the ECMAScript standard is ES2018.

As you can see, there are several ways to refer to the same language. To avoid confusion, when referring to a specific version of the language, it is best to make it clear that you are referring to a specific version of ECMAScript.

Another one is that HTML5 and Javascript are not just different languages but also very different things. The web browser (client) uses HTML5 as its main dialect or language, whereas server-side code uses Javascript for its implementation. JS dates back in 1995 with Douglas Crockford who came up with JS syntax and writing style called The Good Parts .

Features of JS

  • All popular web browsers support JavaScript as they provide built-in execution environments.
  • The syntax and structure of JavaScript are based on the C programming language. As a result, it is classified as a structured programming language.

  • Spaces, tabs, and newlines in JavaScript scripts are ignored by JavaScript.

  • It's a simple and interpretable language.
  • The language is case-sensitive.

Variables

Variables allow computers to dynamically store and manipulate data. They achieve this by pointing to the data with a "label" rather than the data itself. A variable can contain any of the eight data kinds.

Variables are comparable to the x and y variables used in mathematics, in that they're only a term for the stuff we're interested in. Different from mathematical variables, computer variables can store various values at different times.

//For defining a variable in JS we use var keyword
var x = 10;
var y = 20;

To take an input string from console and saving it to a variable, we can write:

var a = readLine()

Now, variable a holds a string which is given as input in the console.

Output “Hello, world!”

To output/print string “Hello, word!” on the console, we can write:

console.log("Hello, world!")

Scopes:

Scope essentially means where these variables are available for use.

There are two types of scopes in JS:

  • Function Scope: Visibility is limited to the function.
function myFn() {

 var x = 10; 

 console.log(x); //prints 10

 } 

 console.log(x); // ReferenceError: x is not defined
  • Block Scope : Visibility is limited to the block of code.
if (true) { 

 let x = 10; 

 console.log(x); //prints 10

 } 

 console.log(x); // ReferenceError: x is not defined

Now that we've established the scope, let's go on to the next step. The scope of var, let, and const can be discussed.

  • var declarations are function scoped.
  • let declarations are block scoped.

  • const declarations are block scoped.

Redefining and Redeclaring feature

A variable declared using ‘var’ can be redefined and even redeclared anywhere throughout its scope.

var x = 30;
 console.log(x); //prints 30
 x = "Hi"; //redefining or re-assigning (works without any error)
 console.log(x); //prints "Hi"

 var y = 10;
 console.log(y); //prints 10
 var y = "Hello"; //Redeclaring (works without any error)
 console.log(y) //Prints "Hello"

A variable declared using ‘let’ can be redefined within its scope but cannot be re-declared within its scope.

let x = 11;
console.log(x); //prints 11
x = "NEO"; //works without any error
console.log(x); //prints "NEO"

let y = 12;
console.log(y); //prints 12
let y = "Vector"; // error: Identifier y has already been declared

let z = 13;
if(true){
 let z = "Joy"; //works without any error as scope is different.
 console.log(z) //prints "Joy"
}
console.log(z) //prints 13

A variable declared using ‘const’ cannot be redefined or re-declared within its scope.

const x = 10;
console.log(x); //prints 10
x = 11; // error: Assignment to constant variable.

const y;
y = 2; //error

const z = 12;
console.log(z) //prints 12
const z= 13; // error: Identifier 'y' has already been declared