WASHINGTON - US President Barack Obama has said both Al-Qaida and ISIS pose a direct threat to the US, observing that "instability will continue for decades" in many parts of the world including Pakistan and Afghanistan.

Obama Tuesday night delivered not-so-veiled jabs at Republican presidential candidates like Donald Trump who have called for keeping Muslims from entering the country, as he defended his legacy while striking an optimistic tone for the future in his last State of the Union address to Congress.

Dealing with foreign policy issues, Obama said "instability will continue for decades" in many parts of the world, including Pakistan and Afghanistan.

"Some of these places may become safe havens for new terrorist networks; others will fall victim to ethnic conflict, or famine, feeding the next wave of refugees," he said in an hour-long address. "The world will look to us to help solve these problems, and our answer needs to be more than tough talk or calls to carpet bomb civilians," he said in a rebuke to Senator Ted Cruz, another Republican presidential candidate, who advocated such a course to destroy the Islamic State. "That may work as a TV sound bite, but it doesn’t pass muster on the world stage."

Obama defended his approach toward the Islamic State, describing the extremist group as a threat to the United States, but not an existential one as suggested by some Republicans. “We don’t need to build them up to show that we’re serious, nor do we need to push away vital allies in this fight by echoing the lie that IS is representative of one of the world’s largest religions,” Obama stated.

Obama also said that the United States should use its standing in the world to solve global problems at a time of rising tensions in places like the Middle East, Ukraine, China and North Korea.

Denouncing anti-Muslim rhetoric, the president said the US needs "to reject any politics that targets people because of race or religion," not as "a matter of political correctness," but to maintain the country's values. A number of representatives of Muslim communities were present in the jam-packed hall.

"It’s a matter of understanding what makes us strong," he said. "The world respects us not just for our arsenal; it respects us for our diversity and our openness and the way we respect every faith."

"When politicians insult Muslims, when a mosque is vandalised, or a kid bullied, that doesn’t make us safer. That’s not telling it like it is," he added. "It’s just wrong. It diminishes us in the eyes of the world. It makes it harder to achieve our goals. And it betrays who we are as a country."

Trump called last month for a "total and complete shutdown of Muslims entering the United States," based on concerns about terrorism. Many Republicans criticised him over the remark, but also joined him in saying refugees from Syria and Iraq should be blocked from the US because they could be terrorists - a sentiment Obama also rejected at the time.

Even South Carolina Governor Nikki Haley, in her Republican response to the State of the Union, seized on the opportunity to take a shot at Trump. She urged Americans to reject the “angriest voices” on immigration. “During anxious times, it can be tempting to follow the silent call of the angriest voices,” Ms Haley, an American of Indian origin. “We must resist that temptation. No one who is willing to work hard, abide by our laws, and love our traditions should ever feel unwelcome in this country.”

In his speech, Obama quoted Pope Francis, who addressed Congress last year and said, "To imitate the hatred and violence of tyrants and murderers is the best way to take their place." The president also rejected the idea that immigrants should be blamed for economic ills, another argument common among many Republicans.

"Immigrants aren’t the reason wages haven’t gone up enough; those decisions are made in the boardrooms that too often put quarterly earnings over long-term returns," Obama said. He came back to the message later, and asked Americans to unite rather than scapegoating others.

"As frustration grows, there will be voices urging us to fall back into tribes, to scapegoat fellow citizens who don’t look like us, or pray like us, or vote like we do, or share the same background," Obama said. "We can’t afford to go down that path. It won’t deliver the economy we want, or the security we want, but most of all, it contradicts everything that makes us the envy of the world."

Earlier in his speech, Obama said that one of the “regrets” of his presidency was that while he ran for office on a platform of unity and change, American politics had become more polarised and divisive during his tenure. “It’s one of the few regrets of my presidency — that the rancor and suspicion between the parties has gotten worse instead of better,” Obama said. “There’s no doubt a president with the gifts of Lincoln or Roosevelt might have better bridged the divide, and I guarantee I’ll keep trying to be better so long as I hold this office.”

Here is the list of Democratic US lawmakers who brought Muslim-American guests to the State of the Union address, and their guests' names: Congresswoman Debbie Wasserman Schultz: brought with her Mohsin Jaffer, a doctor in Weston, Florida; Congressman Keith Ellison, one of the two Muslims in Congress: His son, Elijah, an active-duty combat medic in the Army; Congresswoman Ami Bera: Sarmed Ibrahim, an engineer and Iraqi refugee; Congresswoman Suzanne Bonamici: Salma Ahmad, president of The Islamic Society of Greater Portland; Congresswoman Judy Chu: Adnan Khan, a business owner and past president of the Council of Pakistan American Affairs; Congressman Jim McGovern: Asima Silva, a community leader and software engineer from Central Massachusetts; Congresswoman Grace Meng: (D-N.Y.) Lt Adeel Rana, a New York City Police lieutenant of Pakistani origin; Congressman Seth Moulton: Ahmad Alkhalaf, a 9-year-old Syrian refugee who lost both of his arms in a bombing raid; Congressman Mark Pocan (D-Wis.): Samba Baldeh, a Madison, Wisconsin, alderman and software engineer; Congressman Mike Quigley: Alaa Basatneh, a 23-year-old activist who was born in Syria; Congresswoman Bonnie Watson Coleman: Imam Hamad Ahmad Chebli of the Islamic Society of Central Jersey; Senator Al Franken:  Abdirahman Kahin, owner of Afro Deli & Grill restaurants in Minneapolis and St. Paul, Minnisota; Congressman Dan Kildee: Sarah Hekmati, sister of Amir Hekmati, a former Marine who remains imprisoned in Iran as an accused US spy; and Congressman Matt Salmon: Ramy Kurdi, husband of Sarah Hekmati; Congressman Joe Courtney: Mohammed Qureshi, a doctor and president of the Baitul Aman Mosque in Meriden, Connecticut; and Congresswoman Diana DeGette (D-Colo.): Imam ShemsAdeen Ben-Masaud of the Metro Denver North Islamic Center.