Basic things of JavaScript
Js primary Concept: part 1
First of all welcome to my blog medium world. We know that JavaScript is a programming language so it has types, operators, methods, and built-in functions. Now I will discuss JavaScript types and it,s fundamental concepts of any programming language.
1. Number
Unlike many other programming languages, JavaScript does not define various types of numbers. JavaScript has only double-precision floating numbers. Numbers value are not real integer because it happened float number anytime. For example,
console.log(4/3) //1.33333333
console.log(Math.floor(4/3)) //1
therefore numbers and string add together comfortably as like
var x = 50;
var y = “20”;
var z = x + y; //output will be 5020
2. String
A JavaScript string is zero or more characters written inside quotes. Strings in JavaScript are sequences of Unicode characters. Strings length are found out by length property as like,
const str = 'Hello World'
console.log(str.length); // output will be 11
you can also other string operations like find the first character, to make uppercase also do replace string.
'Hi'.charAt(0); // "H"
'hello, khan'.replace('world', ''); // "hello, khan"
'hi'.toUpperCase(); // "HI"
3. Boolean
JavaScript has a boolean type. Boolean consists of true and false. The number of 0 is express false and 1 expresses true. JavaScript has expressed Boolean function as like example.
Boolean(22); // true
Boolean(''); // false
4. Variables
Variable is a container or like box in which any value can store. There are three kinds of variables type in javaScript. They are var, let, and const. Var keyword use for global declaration, let is to use for scope and const is to be used for strict variable type.
5. Array
An array is one kind of data type. It is not a primitive data type because it comes from an object data type. You must know that function, array, date, and RegExp come from an object. However, array declare as form,
var cat= ["blackcat", "whitecat", "redcat"];
Of course, you declare the value of the array into third curly braces.
6. Function
A function is useful for developers because it makes specific work code for specific needed. It is reusable and you also use a different part of the code.
function add(a, b) {
const total = a+ b;
return total;
}
add(); // output will ne NaN
You can call a function without passing the parameters it expects, in which case they will be set to undefine.
7. Object
Object means real-world things. In programming, language object is important things because programming activities almost depend on the object. Object refers to data structures containing data and instructions for working data.
The numbers, booleans, and strings can be objects if they are declared a new keyword. Objects, functions, arrays, regular expressions, dates, maths are always to be objects.
There are few ways to create objects :
- using new keyword
- using object literal
- using constructor besides EcmaScript5 an object can also be declared with a function.
most common to create an object by literal and there have key-value pairs exists. For Example,
const carInfo= {carName:"John", carSpeed:50km/h,carColor:"blue"};
It can be written a multiline or single line.
8. Operators
Operators basically mean mathematical symbol which produces a result based on two values. There are various kind of operators,
- arithmetic operators
- assignment operators
- string operators
- comparison operators
- logical operators
- type operators
- Bitwise operators
If any developer wants to be strong in fundamental programming language then should be familiar with operators.
9. Conditions
JavaScript condition statements control behavior pieces of code. There are different kinds of conditions in js. The keyword uses this case if, else, else-if, switch.
- If condition: This condition is most common in any programming language. Simple logic build by if condition.
if (10 > 5) {
var outcome = "if condition";
}
outcome; //output will be if condition
- else condition: else condition is associate with if condition. For example,
if ("cat" === "dog") {
var outcome = "if condition";
} else {
var outcome = "else condition";
}
outcome; //output will be else condition
- else-if: You can also extend an if statement with an else if statement, which adds another conditional with its own block.
if (false) {
var outcome = "if condition";
} else if (true) {
var outcome = "else if condition";
} else {
var outcome = "else condition";
}
outcome; //output will be else condition
10. Events
Events basically create interaction on a website. It is so important because without interaction a website seems to be looser. The most example is handling the click event, which is fired by the browser when you click on something with your mouse.
document.querySelector('html').onclick = function() {
alert('alert display');
}
Besides, there are a lot of events in javaScript.