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>
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