Now I feel really excited to write Javascript codes, formally. The tutorial first introduces to the very basic things about Javascript. One of the best thing about Javascript is the logging mechanism which can log any message you want.
console.log("My Message");
This is really helpful in the case where you want to log something when some condition is met or otherwise. This is really a powerful tool I found in Javascript. I can use it to know if there is any error anywhere and pinpoint it.
Unlike any traditional languages I learned before, Javascript has a bit weird (say, unique) way of representing values such as strings and numbers. It has six different types of data-type values such as number, string, boolean, object, function, and undefined. But to store them in a variable, it uses only one keyword i.e. var. This is a mark of a loosely-typed language. The value undefined is stored in any variable which has not been assigned any other data-type value.
var message = "Let's learn Javascript.";
Here, the message variable contains a string type of data.
Numbers are another important datatype which can be represented in various ways.
// (double forward slash) starts a comment var num1 = -13; // integer var num2 = 78.843; // fractional var num3 = 0x59B0; // hexadecimal var num4 = 017; // octal var num5 = 2.995e8; // scientific var num6 = 0b1010; // binary
Javascript uses 64 bits to represent a number. One of the bits is a sign bit and some bits are used to represent after- decimal digits. So the accuracy of representing decimal numbers can’t be achieved but one can represent very large number i.e. The actual maximum whole number that can be stored is in the range of 9 quadrillion (15 zeros).
Strings can be concatenated by using the + operator. Some non-printable, special characters, and Unicode characters can also be represented in a string.
See: Unicode Character Table.
var string1 = 'I can beat "You".';
var string2 = "This is not 'me'.";
var string3 = string1 + " " + string2;
// I can beat "You". This is not 'me'.
var special = "I can beat \"You\"";
// using escape sequence to write "(quote) symbol
var special2 = "You\\Me";
// using escape sequence to write \(backslash) symbol
var special3 = "You and\n\tMe.";
// \n = newline character and \t = tab character
// You and
// Me.
var special4 = "You are a \
good guy, Javascript."
// You are a good guy, Javascript.
var unicode = "\u00E6"; // æ
The boolean data-type has just two value i.e. true and false. Javascript can be used as a calculator or a logical program as it supports various arithmetic operators (+, -, *, /, %), logical operators (&&, ||), unary operators (++. –, -, typeof), conditional operator(?:) and relational operators (>, >=, <, <=, ==, !=, ===, !==).
console.log(null || "user"); // "user" console.log(null && "user"); // null console.log(typeof false); // boolean console.log(typeof 0.1); // number console.log(typeof ""); // string console.log(false ? 1 : 2); // 2 console.log("ba" > 'bZ'); // true
Note 1: Prefer strict equals (===, !==) over simple equal (==, !=) for better type checking before comparison.
Note 2: In most of the cases, null and undefined can be used alternatively. Used mainly to denote an absence of a meaningful value.
Weird Javascript
console.log(null == undefined); // true console.log(null === undefined); // false console.log(null == 0); // false console.log("" == false); // true console.log("" === false); // false console.log(typeof undefined); // undefined console.log(typeof null); // object
Using these basic Javascript features, I could now develop a mini calculator or a logical program that can test logic. The chapter also taught me some weird Javascript pitfalls to stay alert.
Till now, my journey has been smooth and easy. Hope it stays the same.
Thanks for keeping up with me.
Enjoy! 🙂
Things I Learned
- console.log()
- Javascript Syntax
- Javascript value types
- Javascript operators