Let’s get familiar with one of the most popular programming languages…
JavaScript
JS is an programming language introduced in 1995 that lets you add scripting [actions] in webpages.
Here’s its official website.
Aren’t Java and JavaScript the same?
People often think Java and JavaScript are the same language, or that JS is a subset of Java. This is wrong!
JavaScript, prior to release in September 1995, was called LiveScript. During that period, Java was gaining popularity, and so the creators decided to rename it to JavaScript to cruise this wave and here we are.
Hello world
Let’s write our first program in JS. We create a HTML file and add in a <script>
element in there :
<script>
console.log("Hello world!")
</script>
When you open the file in a browser, nothing happens. But, if you use inspect element and go to the console tab…
Well then, hello world and welcome to JavaScript!
console.log()
works similar to Python’sprint()
and Java’sSystem.out.println();
Linking external JS to our HTML file
Just like linking a stylesheet to our HTML file, we can link an external script file to it.
<script src="fileName.js"></script>
Variables
Unlike other languages, JS lets you declare variables in 3 ways [one way is depreciated, by the way].
let one = 54
const two = 77
var three = 14
console.log(one, two, three)
let
lets you define variables.
const
lets you define variables that you can’t change later on. We normally use it to store some hard-coded and known values [like hex-codes, etc.]
var
is similar to let
, but since it’s depreciated we won’t use it much.
You can’t assign a value to a variable AND perform some operation to it at the same time.
JS is a dynamic language
This is a weird quirk of this language. Due to this, variables aren’t directly associated with a particular type. This means you can assign, and re-assign any variable to value of any type.
To prove this, we shall do the following experiment :
let proposedYear = 1984;
console.log(typeof proposedYear); //number
proposedYear = "Smile! Big Brother is Watching You.";
console.log(typeof proposedYear); //string
proposedYear = true;
console.log(typeof proposedYear); //boolean
The same variable has changed its type THRICE and is accepted by JS yet. Wonderful.
Data types? What are they?
Unlike other languages, JS doesn’t have any int
, float
or double
types. Numbers are always stored as double-precision floats [aka double
] and so, it doesn’t matter if they have decimals or not.
let x = 10;
let y = 12.4;
console.log(x, y); //both are the same type
Asynchronous JS
JS is a single-threaded programming language.
Hang on, what is a thread?
A thread is a sequence of actions being executed one after other. For example, this might be your ‘thread’ of execution for making a tea :
- Boil water
- Add tea
- Add milk and sugar
- Heat until done
- Pour tea in cup
- Serve tea
When we talk about JS being a single-threaded language, we mean that JS only has a single call-stack. Like you, JS can only do one thing at a time.
Meanwhile its namesake, Java, happens to be a multi-threaded language. Meaning, it can handle many things at once, like an all-rounder.
JS being asynchronous [anything that operates independent of other processes] means that it can do other menial tasks while waiting for a task to be completed [again, like you!].