Basic Javascript Knowledge
Javascript was created by Brendan Eich in 1995. Its syntax is inherited from the Java and C languages.· Javascript is a
· multi-paradigm,
· dynamic language with types and operators,
· standard built-in objects, and methods,
· object-oriented programming with object prototypes, instead of classes,
· functional programming.
Javascript has no concept of input and output because it was designed as a scripting language to run in the host environment(browsers, Adobe Acrobat, Adobe Photoshop, SVG images, Yahoo’s Widget Engine, server-side environments such as Node js, NoSQL databases like the open-source Apache CouchDB, embedded computers, complete desktop environments like GNOME, and others.)
Types of Javascript:
Number
String
Boolean
Symbol
(new in ES2015)Object
Function
Array
Date
RegExp
null
undefined
Let’s discuss Numbers, Strings:
Numbers:
The JavaScript Number type is a double-precision 64-bit binary format IEEE 754 value. Like double in Java or C#, It can only represent floating-point value, not an integer.
Syntax: Literal syntax and functional syntax are two kinds of syntax in Number.
420; //literal syntax
Number(420); // functional syntax ; Here Number() is a function ,It converts string or other types to Number types.
Create Number syntax: Number() //it creates a number.
Functions for Number:
IsNaN(): IsNaN determines that the value is a number or not by printing True or False
Example:
IsNaN(2) ; // True
IsNaN(hello); // False
parseInt(): parseInt converts a string or binary to an integer. It takes two arguments- one is passed value and the other is the base for the value.
Example:
parseInt(‘567’,10); // 567 ;converting string to integer.
parseInt(‘110’,2); // 5 ;converting binary to integer.
parseFloat(): parseFloat takes an argument and converts a string to a floating-point value.
Example:
parseFloat(‘12.3’);//12.3
Math: Math is a built-in object, not a function object that has static properties and methods for mathematical constants and functions.
Example:
Math.abs(2) ; // 2; returns absolute value.
Math.ceil(4.3); // 5; returns the smallest integer greater than or equal to value.
Math.floor(4.3); // 4 ;returns the largest integer less than or equal to value.
Math.min(2,5,6);// 2 ; returns the smallest of zero or more numbers.
Math.max(2,5,6); // 6 ; returns the largest of zero or more numbers.
Math.random() ; // returns a pseudo-random number between
0
and1
.Math.round(6.89) ; // 7 ; returns the value of the number value rounded to the nearest integer.
Math.sqrt(4); // 16 ; returns the positive square root of value.
Strings:
Strings are used to represent formatted text including characters, integers, and the floating-point unit, sequences of Unicode characters.
Syntax: String literals can be specified using single or double quotes, which are treated identically, or using the backtick character.
data_type variable_name = “ variable_value”;
data_type variable_name = ‘ variable_name’;
data_type variable_name = ` variable_name`;
Example:
const string1 = “ Hello World”;
const string2 = ‘Hello World’;
const string3 =` Hello World`;
Methods for Strings:
charAt(): charAT () accesses information about the string.
Example: ‘Bob’,charAt(1); // ‘o’
lastIndexOf(): lastIndexOf() method returns the index of string of the last occurrence of the specified value, searching backwards from from index. Returns -1 if the value is not found.
Example:
const string1 = ‘ this is a pen’;
const n = string1.lastIndexOf(‘s’); // 6