Getting Started with JavaScript

In this article, I tried to write about very basic key points of JavaScript:

Javascript is a beginner-friendly, dynamic, scripting programming language that can add interactivity to a website. Before starting Javascript, we should have some familiarity with HTML and CSS.

The process of writing code:
- Text editor (VSCode)to write the JavaScript code.
- Web Browser to execute your project code.

Introducing JavaScript —

At first, we have just required a web browser, we can see all the output and can debug our JavaScript code on the web console, we can open the browser console by keyboard shortcut Ctrl+Shift+i

Hello World Program —

Let’s dive into the coding part open any text editor and create the index.html as below —

<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>

<body>
The content of the document.

<script>

console.log("Hello World!");

</script>
</body>
</html>

Expressions and Values in JavaScript —

The following are all valid expressions in JavaScript:

7;
7 + 1; // 8
7 * 2; // 14
'Hello'; // Hello

The value of an expression can be given a name. When we do so, the expression is evaluated first, and the resulting value is assigned to the name. For this, we’ll use the variable.

const hello = 'Hello';
hello; // Hello

Variables in JavaScript —

In JavaScript there are many ways to declare a variable and assign value to them for example var, let, and const.

var —

Initially, this is the only way to declare the variable in JavaScript but ES6 introduces two other way of variable declaration let const .

//These are the some way to declare a variable using var keyword
var name = 'Marjia'
var salary = 5000;
var num = 50;
var bolean = true;

When the var are used outside the function it is globally scoped whereas is var are used inside the function it is available only in that function.

var global = 'Hello world';
function greet(){
var name = 'Marjia';
cosole.log('Hi my name is ' + name );
}

Here variable global are globally scoped whereas the is locally scoped, so if we try to access the outside the function we will get a reference error.

var global = 'Hello world';function greet(){
var name = 'Marjia';
cosole.log('Hi my name is ' + name );}
console.log(name)
//ReferenceError: name is not defined.

let —

Nowadays mostly let is used for the variable declaration as it comes as an improvement over the var .

let greet = 'Hello everyone ';
let count = 10;
if(count > 5){
let hi = 'Say hi instead';
console.log(hi);}

let is the block scope so in the above example we try to excess hii outside the if statement we will get Reference Error.

Just similar to the var , let can be updated but can not be re-declared in the scope. But unlike var , let can’t be re-declared in the same scope.

let name='Tania';
name= 'Marjia //This will work fine
let name='Tania';
let name=' Marjia';//This will give error.

However, if the same variable is declared in a different scope there is no problem.

let count = 20;
if(count>10){
let count = 10;
console.log(count);//Output:10 }
console.log(count);//Output:20

So due to these facts let is more useful than var because with let we don’t need to remember the variable name which is declared in the same scope.

const —

const is declared with const keyword which is usually used to store the constant value that can not be changed later. const also share the same functionality of let , const is also a block scope.

The value ofconst can’t be updated or re-declared

const greet = 'Hi everyone';
greet = 'Hello everyone';//TypeError: invalid assignment to const `greet'

Similar to let if we re-declare in the scope it will give an error.

const greet = 'Hi everyone';
const greet = 'Hello everyone'//SyntaxError: redeclaration of const greet.

Arrays in JavaScript —

JavaScript arrays are used to store multiple values in a single variable. — w3schools

JavaScript’s approach to arrays is a bit different. There isn’t a data type called array in JavaScript. Instead, arrays are objects in JavaScript.

A JavaScript Object can hold different data types (string, integer, boolean) at once:

let person = {
firstName: “John”,
lastName: “Doe”,
age: 20
};// Example of a JavaScript Object

Since an array in JS is an object (with a different syntax), they can also contain different data types:

let array = [1, 2, "hello world", 3.14, true];

Some key points of Array:

  • Each element of an array has a location called index.
  • We can access an element of the array by referring to its index.
  • Array indexes always start with 0, which points to the first element of the array.

JavaScript Objects —

As a very simple definition, objects are nothing but a collection of different types of data. An object can be created with figure brackets {…} with an optional list of properties. A property is a “key: value” pair, where a key is a string (also called a “property name”), and value can be anything.

let school = {
name : "J K School",
location : "Dhaka"
established : "1999"
}

In the above example “name”, “location”, “established” are all “keys” and “J K School”, “Dhaka”, and 1999 are values of these keys respectively.

I hope you found this article helpful if you did give it a 👏, and do share it with your friends and everyone who’s starting with JavaScript.

Thank You!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store